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?
Related
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?
A shell script is not getting executed when using the crontab #reboot option.
I confirmed it by running the below command
grep CRON /var/log/syslog
output:
Aug 4 22:05:57 ubuntu CRON[817]: (root) CMD (/home/a/Workspace/init.sh)
What I did so far:
sudo crontab -u root -e
#reboot /home/a/Workspace/init.sh
init.sh
#!/bin/sh
sudo sh -x /home/a/Workspace/start_zookeeper.sh >> /home/a/log.out &
sleep 10
sudo sh -x /home/a/Workspace/start_kafka.sh >> /home/a/log.out &
sleep 10
sudo sh -x /home/a/Workspace/x-bot/start.sh >> /home/a/log.out &
sleep 10
sudo sh -x /home/a/Workspace/y-bot/start.sh >> /home/a/log.out &
For anyone who is facing a similar issue, I have solved it by using absolute paths and #!bin/sh in all the shell scripts.
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.
if been trouble-checking for hours and can't find out why my shell script won't execute properly when using a root crontab.
I'm on a vServer eqipped with
Ubuntu 14.04.4 LTS
3.13.0-042stab113.11.
my script is a chmod 711 file:
/usr/local/sbin/bckup_script
and looks like this:
#!/bin/bash
DATE=`date +%Y-%m-%d_%H_%M_%S`
su - -c "chgrp postgres /backup/db"
su - -c "chmod 770 /backup/db"
su - -c "chown user /backup/db"
su - postgres -c "pg_dump db_name > /backup/db/${DATE}db_name.sql && pg_dumpall > /backup/db/${DATE}_all_db.out"
su - -c "rsync -a /home/user/value /backup/"
The crontab is started using
crontab -e
as
root
user
The crontab executes as far as I can tell from syslog.
When executed as root user (no crontab), the file will do what it's told to. Also my PATH is set properly and working.
I have no idea what am doing wrong.
Solution:
Thx to Jay jargot I found out what was wrong. To complete the question, here are the outputs you "asked" for:
crontab -l
#m h dom mon dow command
* * * * * bckup_script
Output of crontab was
/bin/sh: bckup_script: command not found
which lead me to the conclusion to use the absolute Path to the file which solved the problem.
my crontab -l now looks like follows and everything works like a charm!
# m h dom mon dow command
49 20 * * 1-5 /usr/local/sbin/bckup_script
Thx very much!
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"'