LinuxQuestions.org
Visit Jeremy's Blog.
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 09-02-2012, 08: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, 08:29 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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, 08: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 08:40 AM.
 
Old 09-02-2012, 09: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 09:06 AM. Reason: Forgot to remove LASTSECS. It was no longer needed.
 
Old 09-02-2012, 11:27 AM   #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, 12: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, 01:12 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 05:49 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 07: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



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

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

All times are GMT -5. The time now is 05:07 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
Open Source Consulting | Domain Registration