Solved

Job scheduled via CRON on Linux does not run

Posted on 2013-10-25
15
567 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySqlDump not dumping triggers 1 29
ftp to port 21 4 40
CENTOS DHCP Server / PXE/TFTP 14 66
how to print sub line with  sed /awk /perl in bash file 11 22
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…
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 …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

815 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

11 Experts available now in Live!

Get 1:1 Help Now