Cron not running for user - linux

I want a user (tcff) to run two python scripts at 2am every morning.
I have correctly installed the following crontab for this user:
tcff#mymachine> crontab -l
0 2 * * * python /home/tcff/path/to/myscript1.py
0 2 * * * python /home/tcff/path/to/myscript2.py
The permissions for each script are:
-rwxr-xr-x 1 tcff tcff 5522 Sep 25 12:41 myscript1.py
-rwxr-xr-x 1 tcff tcff 5522 Sep 25 12:41 myscript2.py
When I call each script directly they work fine:
tcff#mymachine> python /home/tcff/path/to/myscript1.py
[Output as expected]
However they are not being run by cron at 2am each morning.
I can't work this out. I am sure I have the permissions correct etc?

Yes, indeed, the reason the scripts were not running is because I did not use the full path to the Python binary:
0 2 * * * /usr/bin/python /home/tcff/path/to/myscript2.py
This is needed because although the shell (bash) has /usr/bin on the PATH the process running cron does not.

First of all give full permission to script file.
chmod 777 script_name
Also trace the logs of crontab and see what is happening with cronjob here you can see all logs of crontab. May be there is any exception or error.
root#localhost:[~]: tail -f /var/log/cron

Related

Cron.d file not running

I am new to Linux and have a problem that I have tried to look online and could not find a solution
I have 4 scripts that are running in cron.d 3 of them I set to run every minute and they are fine and logging into the output files but the last one should run at 1:00 am but it will not.
-rw-r--r-- 1 root root 390 Jul 29 14:03 Test
* * * * * root /usr/sa/dir1/dir2/script1.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
* * * * * root /usr/sa/dir1/dir2/script2.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
* * * * * root /usr/sa/dir1/dir2/script3.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
0 1 * * * root /usr/sa/dir1/dir2/script4.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
I checked the permission and all seems to be fine as the same script from cron.d file are running as I can see entries from cron that are executed in /var/log/messages and same from the log files.
Things I have tried till now and worked
IF I vim the file and change for the 4th script to run every minute it runs fine.
IF I vim the file and change for the 4th script to run during the day it runs.
IF I include the script under crontab of root user and it runs ok.
IF I run script form the command line it runs ok.
I can not figure out why viming the file in cron.d the script will be executed by cron.
Thank you in advance for the help
Things I have tried till now and worked
IF I vim the file and change for the 4th script to run every minute it runs fine.
IF I vim the file and change for the 4th script to run during the day it runs.
IF I include the script under crontab of root user and it runs ok.
IF I run script form the command line it runs ok.
I can not figure out why viming the file in cron.d the script will be executed by cron.
I don't see errors in /var/log/messages
Are you commiting this crontab by just editing the raw file or are you using crontab <my-new-crontab>? If you aren't then try the latter. :)
I end it up adding the scripts to the crontab of the user root and seems to be ok.

Why is this CRON task not running a Python Program Raspberry Pi

I have a CRON task scheduled as followed:
30 18 * * 1-5 python python /home/pi/myscript.py
Why is it not executing?
it may be a permission issue, try running this in the console
chmod -R 777 /home/pi/myscript.py
The format of writing cron job is incorrect.
Correct entry for this is as following:
30 18 * * 1-5 "python /home/pi/myscript.py"
If you are using Python 3.x then change accordingly i.e.:
30 18 * * 1-5 "python3 /home/pi/myscript.py"

running a script in crontab

I have a very simple script in my crontab that I want to run every day. It is located in /home:
-rwxr-xr-x 1 root root 40 Apr 15 08:01 kill_slony_stop_sql.sh
It has execute permission and here is the content:
#!/bin/bash
slon_kill;rcpostgresql stop
and here is the cron line for it to run daily:
56 12 * * * /home/kill_slony_stop_sql.sh
But it is not working for some reason. When I type /home/kill_slony_stop_sql.sh in the command line, it works good but it is not working in the crontab.
Any thoughts?
It is most likely a PATH issue. Have a look at Why is my crontab not running and be sure to set a PATH so that it can call your slon_kill command.
Also, add some debug to your cron
56 12 * * * /home/kill_slony_stop_sql.sh &>/tmp/errorcron.log
And also look at the logs; cron logs its actions via syslog, which (depending on your setup) often go to /var/log/cron or /var/log/syslog.
I had the same problem with a daily cron job, I used the #daily but this will run at 00:00 every day.
#daily /usr/local/bin/msa70_check.sh
was the cron tab line i added, below is the script i run.
#!/bin/bash
# msa70 disk check
/sbin/mdadm --detail /dev/md0 /dev/md1|
/bin/mailx -s"Disk check on server123 please check" person#domain.com
I also had to edit my script and add /sbin/ and /bin in front of mdadm and mailx for the cron job to run

Why my scheduled job is not working?

I define a job with crontab like this
0 2 * * * dbadmin . /home/dbadmin/back.sh
it is not root I want to run this .sh file with dbadmin user.
but when I checked it is not working.
in the log it gives this:
Feb 22 21:16:01 localhost crond[14634]: (*system*) BAD FILE MODE (/etc/crontab)
Feb 22 21:16:01 localhost crond[14634]: (dbadmin) RELOAD (cron/dbadmin)
Feb 22 21:16:01 localhost crond[28451]: (dbadmin) CMD (dbadmin . /home/dbadmin/back.sh)
How can I fix this? thanks in advance
Make a crontab entry as dbadmin without the username in it:
0 2 * * * /home/dbadmin/movefolder.sh > /home/dbadmin/cron.out 2>#1
Each day the logfile /home/dbadmin/cron.out should be replaced by a new one.
When you are confident about the cron+movefolder, replace the outputfile with /dev/null.
When above fails, check calling the script as dbadmin:
sh /home/dbadmin/movefolder.sh
When this one works and cron fails, it might be the environment. Try saomething like
0 2 * * * . /home/dbadmin/.profile; /home/dbadmin/movefolder.sh > /home/dbadmin/cron.out 2>#1
Using the crontab program, you normally have access only to the 5 scheduling fields (minute, hour, day of month, month and day of week). However, with Vixie cron (usually on Linux) by editing the system crontab file (/etc/crontab, as well as files in /etc/cron.d) you can use the 6th field for the username. For example, see How to specify in crontab by what user to run script?
If you use crontab to enter this line
0 2 * * * dbadmin . /home/dbadmin/back.sh
^^^^^^^
the "dbadmin" username is treated as the command to execute. You can (as noted in crontab's manual page) use that line in /etc/crontab. I pointed out that this is Vixie (also known as ISC) crontab. Legacy systems such as Solaris have a less capable crontab which would not allow specifying the user to run under.
According to cron's manual page, it will send output via email. Perhaps there was no email because the command "dbadmin" failed.
You need to use sh command for executing sh file.like following
0 2 * * * dbadmin sh /home/dbadmin/movefolder.sh
Hope it works.
Thank you.

Scheduling shell script in crontab

I have a shell script that I can run with root user this way:
root#vivid-15:~# ./backup.sh
It's on /root/backup.sh. Now, how do I schedule it on crontab to execute every day at 01:00 AM? I done this:
0 1 * * * root
But now I don't know how to proceed the command to do that.
Have you tried this? Also, a "1" in the hour field means 1am, not 1pm.
0 1 * * * root /root/backup.sh
Edit: changed the 13 (1pm) back to 1 (1am).
Crontab format:
MIN HOUR DAY MON WEEKDAY CMD
I don't know that you need to define what user you want it to run as when its in crontab -- commands will be run as the user who makes the entries with crontab -e. To create a cron process that runs as root, either login as root or set it up with $ sudo crontab -e
I think you're looking for something more like this:
0 1 * * * /root/backup.sh

Resources