D
D
Dmitry2021-09-16 01:03:00
linux
Dmitry, 2021-09-16 01:03:00

How to raise a service using a bash script?

History:
At night, the 1C server starts up, a scheduled task is performed. Scheduled task leaves logs in a file in ANSI encoding. This log is taken, converted, put into the conv.txt file.
Next, the file is parsed by grep, and as soon as the phrase "Completion of the exchange" appears in the log, the 1C server (service) must be paid off (if it happens before 7 am).
For this, a simple script was written (there are no problems with it):

#!/bin/bash

# Текущее время
var_time=`date`
var_hour=`date +%H`

# Час, до которого необходимо выполнять скрипт (обычно 7 утра, т.к. в 7.30 стартует сервер 1С)
dead_hour=21

a=/home/usr1cv8/1c_exchange_logs/reports/Exchange_\(95с1\)
b=`date '+%Y-%m-%d'`

# Входной файл
last_file=$a$b.txt

# Жестко заданный выходной файл конвертированный
conv=$HOME/conv.txt

# Файл логов
log_file=$HOME/shell_logs.txt

# Пишем в лог информацию о запуске скрипта
echo "---------------------------------------------------------"
echo "Скрипт запущен `date +%Y-%m-%d` в `date +%T`"
#echo "Скрипт запущен `date +%Y-%m-%d` в `date +%T`" >> $log_file

if [ -e $last_file ]
then
echo "Файл $last_file существует, запускаем его конвертацию"
iconv -f cp1251 -t utf8 $last_file -o $conv
echo "Файл сконвертирован"
else
echo "Файл $last_file отсутствует."
fi

# Считаем в переменную, сколько совпадений найдено grep'ом
var_grep=`grep -c "Завершение обмена" $conv`

# Если в grep'ом найдено больше нуля совпадений и время меньше чем 7.20 утра, то глушим сервер и делаем об этом запись в лог-файл
if [ $var_grep -gt 0 ]
then
echo "В файле $last_file найдено \"Завершение обмена\" $var_grep раз."
echo "Запускаем проверку времени"

if [ $var_hour -le $dead_hour ]
then
echo "Время меньше 7 утра"
echo "Останавливаем сервер 1С"
sudo service srv1cv83v18-1334 stop
else
echo "Время больше 7 утра"
echo "Сервер 1С остановлен не будет"
fi

else
echo "В файле $last_file не найдено \"Завершение обмена\""
fi

echo "---------------------------------------------------------"

Problem:
The script does not work on cron.
If I run it myself, like:
sh script.sh- everything works fine.
But in which cron I didn’t add it, it doesn’t want to work in any way.
Added it to:
crontab -e
sudo crontab -e
/etc/crontab

Added it to them as:
1  *    * * *   root   /home/user/script.sh
1 * * * * /home/user/script.sh
1 * * * * sudo /home/user/script.sh
1 * * * * sh /home/user/script.sh

Etc. Nothing worked.
Directly cron service both starts and slows down. As a part of a script - in any way.
System:
18.04.1-Ubuntu

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Karabanov, 2021-09-16
@dmitrymp3

Enter absolute paths to utilities and files.
Or set the $PATH and $HOME environment variables.
Cron in Linux: history, usage and device

S
Saboteur, 2021-09-16
@saboteur_kiev

The files seem to be set everywhere with the full path, but I suspect that when you run the cron PATH does not contain everything you need
Ideally, it would be easy to add a script to the cron with redirecting error output and look at the logs, like this
Try either to write the full path to iconv and grep or add redirection of output to the log in the cron and see what the script swears at

1 * * * * /bin/sh /home/user/script.sh>>$HOME/script.log 2>&1

D
Drno, 2021-09-16
@Drno

so you directly in the crown indicate where to write the actions of the script, there you will see the problem, I think

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question