Solved

Job scheduled via CRON on Linux does not run

Posted on 2013-10-25
15
564 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

895 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

15 Experts available now in Live!

Get 1:1 Help Now