LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-01-2011, 08:49 AM   #1
alex64
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Rep: Reputation: 0
cron doesn't start a script


Hi all,
I'm trying to start a script through crond. The record in crontab is: 30 16 * * * /home/alex/razhodilim.pl 1>/dev/null
The script razhodilim.pl works perfectly if I start it manually. But cron fails to start it.
What could be the reason?
 
Old 06-01-2011, 09:06 AM   #2
carrierlogistics
LQ Newbie
 
Registered: Jan 2006
Posts: 1

Rep: Reputation: 0
Make sure you have execute permissions on the file.
 
Old 06-01-2011, 09:08 AM   #3
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Rep: Reputation: 103Reputation: 103
Is cron running as "you"?

E. g. ensure that cron is running as the same user who wrote the script. Also, check that the script is readable and executable as regards permissions, by the user cron is running as.

You can also try running the script via the root user's crontab, just to debug - don't run it like that permanently - if it DOES work, it means you have some form of permissions or owership problem with the script you need to run.

Also, it seems like you're running a perl script? I see no reference to the perl binary. Remember that you might have an environment when you run the script from your terminal, but that cron will not have that same environment and path settings, e. g. it might not be able to automatically find perl to execute the script, you may need to call perl explicitly in your crontab.
 
Old 06-01-2011, 09:08 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The commonest reason for this type of problem is that cron sets up a restricted environment compared with the logon environment; in particular the PATH environment variable is set in a basic way. Your script can set the PATH it needs (which is robust good practice anyway).
 
Old 06-01-2011, 09:13 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Cron "executes" anything that it is supposed to run. The file must therefore be "executable," and furthermore it must be executable by the right user.

The documentation in man cron really is pretty good, once you've also read the various other manpages that are referenced in it. Also, if cron is unable to perform a request for any reason, it will log an error-message in a file that will (probably) be found somewhere within /var/log/messages. A command such as grep -riw cron, issued in that directory by a duly authorized user, will probably be very helpful.
 
Old 06-01-2011, 11:00 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Also, if cron has an issue, it generally emails the cron user (or possibly root).
You could also change
Code:
1>/dev/null
to
Code:
1>/tmp/mylog 2>&1
to see if its trying to tell you something.

As per post#4, cron has a restricted environment; ensure you set/source any vars you need, with the absolute/full path to all files referenced (or start by setting the right env).

@rylan76: you don't need to specify perl in the cmd, just set it at the top as the first line of code, similar to other scripting style langs eg

Code:
#!/usr/bin/perl -w
 
Old 06-03-2011, 01:29 AM   #7
alex64
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Hi,
Thank you all for the instructions. The file razhodilim.pl is executable, it runs without problems if started manually.
The crond is started by root.
There were no mails.
I tried to redirect the output to a file as Chris suggested but the log file is empty.
In the /var/log/cron file there is a record : USER root pid 6598 /home/alex/razhodilim.pl 1>/home/alex/error 2>&1
I also tried setting PATH variable with the paths I have in the login environment - nothing changed.

Any ideas?

Alexander
 
Old 06-03-2011, 05:56 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Two ideas: 1) start crond at a higher logging level and 2) try scheduling a very simple bash, ksh or sh script that does nothing except echo running > /home/alex/error

Probably irrelevant as the script runs from the command line but what is its first line, probably a shebang line?
 
Old 06-03-2011, 08:35 AM   #9
alex64
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Hi catkin,
I restarted cron with logging level 12, wrote the script test.sh which echoes in /home/alex/error and scheduled it in crontab. The cron executed it. There was no log in /var/log - cron stopped logging at all.
The first line of razhodilim.pl is #!/usr/bin/perl
 
Old 06-11-2011, 12:27 AM   #10
alex64
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Original Poster
Rep: Reputation: 0
The script has to open a file in /home/alex and it wasn't able to find it if run through cron. I put the full path in the script and that solved the problem.
Thank you all!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cron doesn't run a certain script mcdorians Linux - Software 12 04-04-2011 02:35 PM
cron daemon doesn't start from inittab, but says already running!! AbrahamJose AIX 1 09-19-2008 04:52 AM
cron entry doesn't start kpachopoulos Linux - General 3 01-29-2008 06:46 PM
script doesn't run in cron (runs from shell just fine) Timur Sakayev Linux - Software 6 02-26-2007 12:56 PM
using cron to start and stop a bash script monty Linux - Software 2 03-20-2006 02:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration