crontab taks not running - bash

signed in as root user, crontab -e has a single line entry:
*/1 * * * * /opt/launch-crashed-services.sh > /dev/null 2>
the /opt directory has the following permissions defined for that script
-rwxr-xr-x 1 root root 179 Sep 20 06:47 launch-crashed-services.sh
which should thus run every minute a check that nginx is running and restart if not, dumping the output to a log file.
service nginx status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
sudo service nginx restart > /var/log/nginx/relaunch.log # /dev/null
fi
The status of nginx is frequently inactive for long periods & said log file is empty.
Thus this script is either not being fired or somehow fires, but incorrectly.
Where is this set-up mistaken?

Related

Run a sh with Xvfb display using cron?

I am trying to run a sh script using cron. This script requires a display.
I have tried:
# Xvfb display
/usr/bin/Xvfb :10 -ac -screen 0 1024x768x24 &
41 18 * * * /bin/sh /path/to/script/script.sh > /path/to/log/log.log 2>&1
Nothing happened and the log.log was not created.
Second try:
43 18 * * * <user> export DISPLAY=:10 /bin/sh /path/to/script/script.sh > /path/to/log/log.log 2>&1
Same result no log file.
(ubuntu) CMD (export DISPLAY=:10 /bin/sh /ibc.paper/twsstart.sh > /crontry.log 2>&1)
Jun 14 18:43:01 CRON[1809]: (CRON) info (No MTA installed, discarding output)
I have been trying all the things I could think of but nothing works.
Can someone tell me what to check?

Check if file exists remotly in bash

I have a bashscript which checks if a file exists on the remote server.
When i execute this bashscript on commandline it works fine and say to me the file exist (as it should). But when crontab is executing this bashscript it says that the file not exist (although it would exist
).
can anybody help me?
myscript.sh
#!/bin/bash
if $(sudo ssh -i <path/to/ssh/keys> <user>#<ip> "[[ -f /etc/ssl/file.txt ]]");then
echo "exist"
else
echo "not exist"
fi
crontab:
*/1 * * * * bash /home/user/myscript.sh | mail -s "betreff" user#email.com
stderr: (when i run the script on the commandline)
++ sudo ssh -i <path/to/ssh/keys> <user>#<ip> '[ -f /etc/ssl/file.txt ]'
+ echo exist
exist
stderr: (when i run the script in cron)
++ sudo ssh -i <path/to/ssh/key> <user>#<ip> '[ -f /etc/ssl/file.txt ]'
Warning: Identity file /root/.ssh/key/keyfile not accessible: No such file or directory.
Permission denied, please try again.
Permission denied, please try again.
root#<ip>: Permission denied (publickey,password).
Permission of ssh keyfile:
-rw------- 1 root root 3243 Sep 30 15:34 keyfile
-rw-r--r-- 1 root root 741 Sep 30 15:34 keyfile.pub
Thanks for helping :D

Start nginx from shell script on crontab

I want to check NGINX is running or not every 1 minute.
My shell script is:
#!/bin/sh
ps auxw | grep nginx | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "NGINX is not running"
/etc/init.d/nginx start
else
echo "NGINX is running"
fi
Script run with sh launch.sh correctly (If NGINX is not running, run NGINX).
The problem is when I want to run the script every 1 minute by crontab, nothing happens. Crontab list is here:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * ~/sh launch.sh
I test * * * * * sh launch.sh, * * * * * launch.sh and * * * * * ./launch.sh but none of them work correctly.
My OS is UBUNTU 18.04.
This is log:
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3239]: (root) CMD (~/launch.sh)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3240]: (hajitsu) CMD (/home/hajitsu/launch.sh)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3238]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3237]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3374]: (root) CMD (~/launch.sh)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3373]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3376]: (hajitsu) CMD (/home/hajitsu/launch.sh)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3372]: (CRON) info (No MTA installed, discarding output)
I think the command fired but nothing happend.
NGINX needs sudo privilege.
If you have sudo privileges you can modify /etc/sudoers.d/username file and execute sudo commands without password.
The file usually contains a user and a list of commands that the user can run without having to specify a password. In your case, you can run:
sudo /etc/init.d/nginx start
Add or modify your sudoers file. (replace username with your username.)
$ EDITOR=nano sudo visudo -f /etc/sudoers.d/username # EDITOR=nano sets my editor (because I am more comfortable with nano)
Copy and paste following.
You can add more sudo commands separating by comma.
username ALL=(ALL) NOPASSWD: /etc/init.d/nginx start,/etc/init.d/nginx start
Note: Commands will only execute called with sudo.
Prepend sudo in your launch.sh:
#!/bin/sh
ps auxw | grep nginx | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "NGINX is not running"
sudo /etc/init.d/nginx start
else
echo "NGINX is running"
fi
Make file executable.
$ chmod +x launch.sh
~ won't be expanded the way it is in an interactive shell when in a crontab. Use /home/username instead.

sh script works manually but not via cron

I'm trying to run a sh script from crontab. If I run script manually it works perfect, but when I run it in crontab, I get errors.
The script:
#!/bin/bash
sudo tar -zcvf /var/www/nextcloud/data/nextcloud/files/backup.tar.gz /home/beno/stuff/
sudo -u www-data /usr/bin/php /var/www/nextcloud/occ file:scan --all >> /var/www/nextcloud/data/nextcloud/files/backup_log.txt
The script is supposed to make a tar backup of a folder and put it in nextcloud folder and run command files:scan, so nextcloud rescans filesystem and starts synchronization...as I read it here:
https://doc.owncloud.org/server/9.0/admin_manual/configuration_server/occ_command.html#file-operations-label
When crontab runs the script, backup.tar.gz is created, then I get following error:
An unhandled exception has been thrown:
Doctrine\DBAL\DBALException: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory in /var/www/nextcloud/lib/private/DB/Connection.php:60
I'm using ubuntu16 and nextcloud11. Please help!
Following you'r request
Instead of using you'r own crontab, you have to use specic user's crontab, by sudo your crontab command:
sudo -s
then
crontab -e
now you coul add the root's 1st line:
tar -zcvf /var/www/nextcloud/data/nextcloud/files/backup.tar.gz /home/beno/stuff/
and once finished
exit
sudo -s www-data
... and so on...
or (if you could'nt run a shell under www-data):
sudo www-data crontab <<<'01 2 * * * /usr/bin/php /var/www...'
Note: This will overwrite www-data's crontab by this only line!
To prevent this, you could:
sudo www-data crontab -l
to see what contain actual crontab, then
sudo www-data crontab -l |
sed -e '$a 01 2 * * * /usr/bin/php /var/www...'
For adding you'r line, and finally
sudo www-data crontab -l |
sed -e '$a 01 2 * * * /usr/bin/php /var/www...' |
sudo www-data crontab
For replacing actual crontab by modified one.
But it could be simplier to
sudo vi /etc/cron.d/mybackups
Then add you'r rule by following time spec by user name:
21 22 * * * root tar -zcvf /var/w...
22 23 * * * www-data /usr/bin/php /var/www...
Further doc
see man -P'less +/SYSTEM' 5 crontab

Cronjob command starts 2 processes, for 1 python script

My crontab had the command:
50 08 * * 1-5 /home/MY_SCRIPT.py /home/arguments 2> /dev/null
59 23 * * 1-5 killall MY_SCRIPT.py
Which worked perfectly fine, but when I used to do
ps aux | grep SCRIPT
It showed:
myuser 13898 0.0 0.0 4444 648 ? Ss 08:50 0:00 /bin/sh -c /home/MY_SCRIPT.py /home/arguments 2> /dev/null
myuser 13900 0.0 0.0 25268 7384 ? S 08:50 0:00 /usr/bin/python /home/MY_SCRIPT.py /home/arguments
Why are 2 processes been shown?
And the killall command also used to work fine.
I made a change to my script and in order to get the new behaviour, I had to kill the currently running scripts and I used
kill 13898 13900
After that I used the same command (as in crontab)
/home/MY_SCRIPT.py /home/arguments 2> /dev/null
But now after restarting the script, it showed only 1 process (which makes sense)
Everything looks good till here, but this time the killall MY_SCRIPT in the cronjob didnt work, it said could not find pid. And the script kept on running until I had to manually kill it.
Need to find out the reason for this behaviour:
Why 2 processes from cronjob
Is there something wrong the way I restrated the script
How do I make sure that next time I restart the script, the cron should kill it for sure
OS:Linux Ubuntu
You're seeing two processes because crontab uses /bin/sh to summon your python script. So basically what happens is:
/bin/sh -c '/home/MY_SCRIPT.py /home/arguments 2> /dev/null'
And the process structure becomes
/bin/sh -> /usr/bin/python
Try this format instead:
50 08 * * 1-5 /bin/sh -c 'exec /home/MY_SCRIPT.py /home/arguments 2> /dev/null'
It may also be a good idea to specify the full path to killall. It's probably in /usr/bin. Verify it with which killall.
59 23 * * 1-5 /usr/bin/killall MY_SCRIPT.py
Another more efficient way to do it is to save your process id somewhere:
50 08 * * 1-5 /bin/sh -c 'echo "$$" > /var/run/my_script.pid; exec /home/MY_SCRIPT.py /home/arguments 2> /dev/null'
And use a more efficient killer:
59 23 * * 1-5 /bin/sh -c 'read PID < /var/run/my_script.pid; kill "$PID"'

Resources