LinuxQuestions.org
Visit Jeremy's Blog.
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 02-09-2015, 02:39 PM   #1
getraj.12
LQ Newbie
 
Registered: Jul 2010
Location: Bangalore, India
Distribution: RedHat, Suse, Solaris
Posts: 15

Rep: Reputation: 0
Script not getting executed via CRON


I have a small script1 scheduled via cron to run every 20 mins to invoke script2 if script2 is not running.
When i run script1 from cmd line it works fine but when it is scheduled via cron it doesn't work. Not sure what am i doing wrong here.
I even tried to use absolute path.

Script 1
Code:
#!/bin/sh
/bin/ps -ef |/bin/grep script2.sh |/bin/grep -v $$ > /dev/null 2>&1
if [ $? -ne 0 ]
then
/usr/bin/nohup /home/user1/script2.sh &
fi
CRON LOG :
Feb 9 17:00:01 server1 crond[29771]: (user1) CMD (/home/user1/chk_script1.sh >/dev/null 2>&1)
Feb 9 17:20:01 server1 crond[21095]: (user1) CMD (/home/user1/chk_script1.sh >/dev/null 2>&1)
Feb 9 17:40:01 server1 crond[11218]: (user1) CMD(/home/user1/chk_script1.sh >/dev/null 2>&1)
Feb 9 18:00:01 server1 crond[29961]: (user1) CMD(/home/user1/chk_script1.sh >/dev/null 2>&1)

CRON JOB :
00,20,40 * * * * /home/user1/chk_script1.sh >/dev/null 2>&1
 
Old 02-09-2015, 02:41 PM   #2
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
I see some redirects to /dev/null

While I don't think this is the cause of your problem, consider removing it until the problem is solved.
More debug info is always better when you can't figure out what's going on!

Last edited by Miati; 02-09-2015 at 02:52 PM.
 
Old 02-09-2015, 03:00 PM   #3
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Code:
if [ $? -ne 0 ]
is always false because you are evaluating whether the redirect to /dev/null was successful. Which it probably was.
 
Old 02-09-2015, 03:07 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by joe_2000 View Post
Code:
if [ $? -ne 0 ]
is always false because you are evaluating whether the redirect to /dev/null was successful. Which it probably was.
That's not true, the redirection isn't its own command and doesn't affect the exit status
Code:
$ ls > /dev/null 2>&1
$ echo $?
0
$ ls sfsdfdssd > /dev/null 2>&1
$ echo $?
2
You said it "doesn't work", well what about it doesn't work? Is the ps | grep not working? Is it not launching script2? Is it ALWAYS launching script2? What about it is misbehaving?

And as Miati mentioned, get rid of the /dev/null redirects until you know what the problem is. You're throwing away all of your debugging information.

Last edited by suicidaleggroll; 02-09-2015 at 03:08 PM.
 
1 members found this post helpful.
Old 02-09-2015, 03:12 PM   #5
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
Originally Posted by suicidaleggroll View Post
That's not true, the redirection isn't its own command and doesn't affect the exit status
My bad. I actually tested this but now I can see I tested with a wrong example. Thanks for correcting.
 
Old 02-09-2015, 03:12 PM   #6
getraj.12
LQ Newbie
 
Registered: Jul 2010
Location: Bangalore, India
Distribution: RedHat, Suse, Solaris
Posts: 15

Original Poster
Rep: Reputation: 0
THanks for the update joe_2000 and Miati. I will remove the redirects.

does the below lines will check the exit status of the previous command ? Because the this script (chk_script1) should invoke script2 should execute only if script2 is not running and if it is running already it should not try to ivoke 2nd time. Is there any other way to achieve this ?

Code:
#!/bin/sh
/bin/ps -ef |/bin/grep script2.sh |/bin/grep -v $$ 
if [ $? -ne 0 ]
then
/usr/bin/nohup /home/user1/script2.sh &
fi

Last edited by getraj.12; 02-09-2015 at 03:19 PM.
 
Old 02-09-2015, 03:22 PM   #7
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
Originally Posted by getraj.12 View Post
Is there any other way to achieve this ?
I usually use a lock file. Here is a snippet that you can use:
Put the following script int /usr/local/include and call it lock_process.sh:
Code:
 #!/bin/bash
 
 function lock() {
   if [ $# -ne 1 ];then
     "Process Name missing!"
     exit -1
   fi
   NAME=$1
   LOCKFILE=/tmp/$NAME.lock.$BASHPID
   if [ -f $LOCKFILE ];then
     echo "Lockfile $LOCKFILE exists!"
     exit -1  
   else
     for PREV_LOCK_FILE in  /tmp/$NAME.lock.*
       do
         if [ -f $PREV_LOCK_FILE ]; then
           echo "Lockfile $PREV_LOCK_FILE from previous process exists"
           PREV_PID=$(echo $PREV_LOCK_FILE | cut -d'.' -f 3 )
           echo "Checking for process id $PREV_PID"
           if [ -e /proc/$PREV_PID ];then
             echo "Process of $PREV_PID is still running!"
             exit -1
           else
             echo "Process seems dead. Remove lock file"
             rm $PREV_LOCK_FILE
           fi
         fi
       done
     touch $LOCKFILE
   fi
 }
 
 function unlock() {
   if [ $# -ne 1 ];then
     "Process Name missing!"
     exit -1
   fi
   NAME=$1
   LOCKFILE=/tmp/$NAME.lock.$BASHPID
   rm $LOCKFILE
 }
Then, in another script, include it at the beginning:

Code:
source /usr/local/include/lock_process.sh
At the beginning of the other script, create a lock file (replace "my_lock_name" with something sensible):

Code:
lock "my_lock_name"
And remove it at the end:

Code:
unlock "my_lock_name"
Any other script using the same lock name will terminate with an error when trying to lock if the lock is already taken by another script. If the other script took a lock and did not release it, but is not running anymore (due to abnormal termination) the lock file will be removed when you try to obtain the new lock.
You can either use this or understand how it's working to adapt it to your needs...
 
Old 02-09-2015, 03:27 PM   #8
joe_2000
Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 808

Rep: Reputation: 216Reputation: 216Reputation: 216
Alternatively, I guess you could also just do

Code:
if ! pgrep script2.sh;then 
      /usr/bin/nohup /home/user1/script2.sh &
fi
 
Old 02-09-2015, 03:27 PM   #9
getraj.12
LQ Newbie
 
Registered: Jul 2010
Location: Bangalore, India
Distribution: RedHat, Suse, Solaris
Posts: 15

Original Poster
Rep: Reputation: 0
I have also removed /dev/null redirect from cron job. I do not find anything in the logfile as well.
 
Old 02-10-2015, 06:17 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Instead of the re-directs to null, re-direct all o/p to a logfile ie remove redirects inside script1 (& script2) and capture all o/p at the script1 cron entry level into a logfile.
Also, add 'set -xv' to the top of both script files.
Post script2 as well - we may see something in there
 
Old 02-10-2015, 01:57 PM   #11
getraj.12
LQ Newbie
 
Registered: Jul 2010
Location: Bangalore, India
Distribution: RedHat, Suse, Solaris
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks a lot for your prompt responses.
The issue i am facing is with script1 (Mentioned below) I have already tried to redirect the outputs to a file and the files are blank... doesn't show up anything logs. I have no issues with script2. script1 is just to check if script2 is already running and if not running it just needs to kick off script2.
I can confirm the cron job executes fine and just that it doesn't kickoff script2. I suspect the exit status check (if [ $? -ne 0 ]) from the script is not validated/working when it is run via cron(please correct me if i am wrong) as the first line of the script does change the timestamp of the logfile when it is run via cron.
Code:
#!/bin/sh
/bin/ps -ef |/bin/grep script2.sh |/bin/grep -v $$ > /home/user1/check_script1.log 2>&1
if [ $? -ne 0 ]
then
/usr/bin/nohup /home/user1/script2.sh &
fi
log file : /home/user/ceck_script1.log
user1 22905 22900 0 15:20 ? 00:00:00 /bin/sh -c /home/user1/chk_script1.sh > /home/user1/check_script1.log 2>&1

Does anyone have better idea to accomplish what i need to do using script1.
Thank you
 
Old 02-10-2015, 02:25 PM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Get rid of the redirects inside the script. Let them print to stdout, and let the cronjob redirect handle the logging. From what I can tell, both the cron job and the commands inside the script itself are writing to the same log file, and both are set to replace the existing contents.

Your ps grepping is not very robust, I can see numerous ways in which it would fail. All you're doing is grepping for "script2.sh". This will match ANYTHING with script2.sh in the name or arguments. Perhaps you have a "vi script2.sh" open, or an "scp script2.sh", or anything else that contains the string "script2.sh". You should be recording the PID of script2.sh when it's launched and use that in the ps filtering as well.

Last edited by suicidaleggroll; 02-10-2015 at 03:00 PM.
 
  


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
[SOLVED] Configured Cron job executed every hour is instead executed every minute for 10m markings Linux - Software 4 05-13-2012 06:43 PM
Script runs if executed from CL, not from Cron Ravendark Programming 6 10-13-2010 07:07 AM
how can we know whether a Cron job has been executed or not ?? vikas027 Linux - Server 6 04-17-2009 02:49 PM
bash script runs different when cron executed jalder85 Linux - Server 4 02-20-2009 12:53 PM
script not executed by cron.daily hopefull Linux - Newbie 6 01-17-2009 10:54 AM


All times are GMT -5. The time now is 10:23 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