Solved

Job scheduled via CRON on Linux does not run

Posted on 2013-10-25
15
573 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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.

840 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