Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.