LinuxQuestions.org
Help answer threads with 0 replies.
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 09-02-2012, 09:26 AM   #1
Deepesh_tr
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
Script is not running in Crontab


I have scheduled a script in crontab for every minute with all ***** stars but this script is not running , manually i am able to run the same script successfully , i am running in the same shell that is used in my script.

CMD execution can be shown in logs of cron but crontab is not executing as per the scheduled time.

please suggest
 
Old 09-02-2012, 09:29 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,821

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
If you have to run something every minute, then I suggest writing the minute-loop into the script itself. Have the script take care of the timing.
 
Old 09-02-2012, 09:38 AM   #3
Deepesh_tr
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
i have not included minute loop in the script but i tried to write the simple hello word program also and scheduled in cron for every min that is also not working for me

do i need to route the o/p of script on some terminal explicitly? , can you please cite an example for successful hello world script that runs in every min via cron?

Last edited by Deepesh_tr; 09-02-2012 at 09:40 AM.
 
Old 09-02-2012, 10:04 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Perhaps cron runs the command only every after a minute after the script has finished executing, so that means it's not going to run exactly a minute after each start time. If there's no other way to configure that properly in cron, perhaps it would really be best if you run a daemon script that would execute the command every minute.

As an example:
Code:
#!/bin/bash

declare -i CURRENTSECS NEXTSECS

NEXTSECS=$(exec date +%s)

until
	CURRENTSECS=$(exec date +%s)
	
	if [[ CURRENTSECS -ge NEXTSECS ]]; then
		# execute command to background
		: here &

		(( NEXTSECS = NEXTSECS + 60 ))
	fi

	read -t 1 -n 1 && [[ $REPLY == [qQ] ]]
do
	continue
done
Note: 'read' might not work if used in a non-interactive session. So you just have to use sleep instead:
Code:
#!/bin/bash

declare -i CURRENTSECS NEXTSECS

NEXTSECS=$(exec date +%s)

for (( ;; )); do
	CURRENTSECS=$(exec date +%s)
	
	if [[ CURRENTSECS -ge NEXTSECS ]]; then
		# execute command to background
		: here &

		(( NEXTSECS = NEXTSECS + 60 ))
	fi

	sleep 1s
done
Modifications has to be made if you want to make sure that you don't run multiple instances of the child command at once.

Last edited by konsolebox; 09-02-2012 at 10:06 AM. Reason: Forgot to remove LASTSECS. It was no longer needed.
 
Old 09-02-2012, 12:27 PM   #5
KinnowGrower
Member
 
Registered: May 2008
Location: Toronto
Distribution: Centos && Debian
Posts: 347

Rep: Reputation: 34
Can you show the entry in the crontab?
 
Old 09-02-2012, 01:14 PM   #6
iamwilliam
Member
 
Registered: Apr 2006
Location: Nairobi
Distribution: CentOS
Posts: 78

Rep: Reputation: 21
Hi,

Try debug this by redirecting the cron output to a file
Code:
* * * * * /path/to/script/script.sh 2>&1 >> /tmp/logfile.txt
I've experienced something similar before. Turns out its almost always a problem with $PATH in the cron environment.
 
Old 09-02-2012, 02:12 PM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
And check /var/log/syslog to see if your script ran at all or not.
Did you put an empty line as last line in crontab?

jlinkels
 
Old 09-02-2012, 06:49 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
iamwilliam's technique is a good idea. As hinted, cron runs in a restricted environment, so always use the full path to any cmd or file or supply the relevant PATH etc yourself explicitly in the code.
Also, add
Code:
set -xv
at the top , which will show you what the parser is doing.
Note also that cron is a detached process, ie has no cxn to any terminal sessions
 
Old 09-02-2012, 08:43 PM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Btw, if you are to run a deamon, it's best if you just use another higher language instead, just to save IO binary read/syscalls. Like Perl, Python or Ruby.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Running a script as a different user from crontab rainjam Linux - Newbie 1 06-15-2012 08:06 AM
Crontab not running script j8177e5 Linux - Newbie 2 12-21-2010 09:54 AM
[SOLVED] Crontab Script Not Running th1bill Ubuntu 4 12-10-2010 04:49 PM
crontab not running script sunlinux Linux - Newbie 5 05-18-2010 07:21 AM
Running a script with crontab. glore2002 Slackware 3 06-05-2008 09:48 PM


All times are GMT -5. The time now is 04:27 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration