Solved

Job scheduled via CRON on Linux does not run

Posted on 2013-10-25
15
561 Views
Last Modified: 2013-11-11
I scheduled a job via CRON on Linux server:

>crontab -e

30 13 * * * /root/myscript.ksh 2>&1  >> /root/myscript.log

The job did not run and nothing got written to the log file

What am I doing wrong?
0
Comment
Question by:YZlat
15 Comments
 
LVL 34

Expert Comment

by:Seth Simmons
ID: 39601067
does it run manually?
is cron service running?
0
 
LVL 13

Assisted Solution

by:Daniel Helgenberger
Daniel Helgenberger earned 117 total points
ID: 39601075
Who's the user you are using crontab -e with?
Cron logs may be in /var/log/cron - depending on your distro.
Also, put this line in a file in /etc/cron.d/ - this ensures it is run by root.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39601080
For cron jobs you need to:

1- use full path names to file and folders
2- set env variables needed, since cron will not read .profile, etc. e.g. you need to set PATH
3- redirect output & errors (stdout & stderr) to files
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39601088
also, make sure that script file is executable and you set shell to be used in the script itself, e.g. put this at your script:

#!/usr/bin/ksh
0
 
LVL 35

Author Comment

by:YZlat
ID: 39621585
already using #!/usr/bin/ksh


What do I set PATH to?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39621611
You need to set it to the directories where your binaries / commands are. e.g.

PATH=/usr/bin:/path/to/my/commands

You could simply take it from your login shell:

echo $PATH

This should give you the PATH you have when you login to system and then you set it to the same in your shell script (and export it with: export PATH).
0
 
LVL 35

Author Comment

by:YZlat
ID: 39621703
how do I find out the directory for the binaris/commands?

Also how do I find out which user cron runs under?

I am brand new to linux

P.S> I moved my script to /etc/cron.d as has been suggested:

30 13 * * * /etc/cron.d/myscript.ksh 2>&1  >> /etc/cron.d/myscript.log

but the script does not seem to run and myscript.log file still does not get generated
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Assisted Solution

by:omarfarid
omarfarid earned 233 total points
ID: 39621779
Ok, in your question you said:

"I scheduled a job via CRON on Linux server:

>crontab -e

30 13 * * * /root/myscript.ksh 2>&1  >> /root/myscript.log"

The user that crontab job is scheduled under is the same user logged in and ran crontab -e

you could find username by running command

username

or running command

id

As I mentioned before, while logged in run command

echo $PATH

this sould give your directories under which binaries or commands are located (dir names are separated by : )
0
 
LVL 35

Author Comment

by:YZlat
ID: 39621828
thanks, I know I can do echo $PATH, but when running a script via crontab I need to set PATH inside the script and export it. What do i set it to? or do i just do export PATH?


But even then it still does not explain why cronta does not run the job or even generate a log file
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 233 total points
ID: 39622091
e,g,

when you run

echo $PATH

you will get something like

/usr/bin:/sbin:/usr/user1/bin:/usr/local/mycmd

then you can add to your script line like

PATH=/usr/bin:/sbin:/usr/user1/bin:/usr/local/mycmd ; export PATH
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39622093
do you have other crontab jobs? check with

crontab -l

to see if other crontab jobs are scheduled
0
 
LVL 35

Author Comment

by:YZlat
ID: 39638921
nope, no other cron jobs - it is a new server
0
 
LVL 35

Author Comment

by:YZlat
ID: 39639101
setting $PATH didn't make a difference:(
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39639215
can you run simple crontab job and see if you get it run?

can you share the script you are trying to run?
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 39639758
thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now