LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script is not running in Crontab (https://www.linuxquestions.org/questions/linux-newbie-8/script-is-not-running-in-crontab-4175425253/)

Deepesh_tr 09-02-2012 08:26 AM

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

szboardstretcher 09-02-2012 08:29 AM

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.

Deepesh_tr 09-02-2012 08:38 AM

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?

konsolebox 09-02-2012 09:04 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.

KinnowGrower 09-02-2012 11:27 AM

Can you show the entry in the crontab?

iamwilliam 09-02-2012 12:14 PM

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.

jlinkels 09-02-2012 01:12 PM

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

chrism01 09-02-2012 05:49 PM

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

konsolebox 09-02-2012 07:43 PM

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.


All times are GMT -5. The time now is 06:03 PM.