How to determine the script is called from CRON?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to determine the script is called from CRON?
# 8  
Old 06-15-2005
Quote:
Originally Posted by wes_brooks
What I want is to determine whether the script is called from CRON or it is executed interactively?
... i'm no expert but ...

if you're running the script or program on the command line by yourself --- it's running interactively

if the script is regularly running at specific times (i.e., at 1 PM every day) without your intervention or anybody else's --- it's most likely being run by cron (see "man cron")
# 9  
Old 06-15-2005
Smilie

What he meant was he wants his script to determine if it running via cron so that it can take different actions. For example an interactive script might give a progress report and request permission to proceed to phase 2. An interactive script might just send errors to fd 2 while a cron run script might:
mv /var/log/somescript/errors /var/log/somescript/errors.old
exec 2> /var/log/somescript/errors
or something like that.
# 10  
Old 06-15-2005
now i get it ...

have the script check for $TERM --- if set, the script is most likely running interactively ... if not, the script is most likely running through cron ...

Code:
#! /bin/ksh

if [ $TERM ]
then
    echo "I'm running interactively."
else
    echo "I'm running through cron" | mailx -s "cron test" user@domain.com
fi

exit 0

# 11  
Old 06-15-2005
Code:
#!/bin/ksh
if [ -t 0 ] ; then
   echo "I am interactive"
else
   echo "I have no controlling terminal - cronned?"
fi;

# 12  
Old 06-15-2005
Quote:
Originally Posted by Just Ice
now i get it ...

have the script check for $TERM ---
That was what I said in #3.
# 13  
Old 06-15-2005
Quote:
Originally Posted by tom_xx_hu@yahoo
That was what I said in #3.
... my apologies ... my wrong interpretation of the question didn't help my understanding of what people have posted ... going back through the posts now ... pixelbeat posted similar code but i missed that too ... seems i need new glasses or at least a brain that works ...
# 14  
Old 06-28-2005
Error same prob but difference is not interactive its rc script

Hi,

Am new and got registered only for this one thread...... Smilie
Well got the same prob. but i need to differentiate if my script was invoked by rc script or by cron.....i used this but prob is that the parent of my perl script is shown as shell (bash) and not cron and that is not what i want.....how do i get around this??????

$cronpid=`ps -ef|grep cron| awk '{if(NR==1) print \$2;}'`;
$parentpid=`ps -ef | grep action.pl |awk '{if(NR == 1) print \$3; }'`;
$printst=`ps -ef| grep action.pl`;
print "Output of ps is\n$printst\n ";
print "************Cron pid is $cronpid and parentpid is $parentpid\n**************";
$printst1=`ps -ef | grep $parentpid`;
print "My parent is\n $printst\n";
if($cronpid == $parentpid){
print "This process was invoked by cron...it cannot reboot machine";
$rebootnow=2;
}
else{ print "Rebooting.....";
}

Output shown is---------------------
Output of ps is
pooja 10890 8350 0 16:43 pts/0 00:00:00 /usr/bin/perl ./action.pl
pooja 10915 10890 0 16:43 pts/0 00:00:00 sh -c ps -ef| grep action.pl

************Cron pid is 7877
and parentpid is 8350

**************My parent is
pooja 10890 8350 0 16:43 pts/0 00:00:00 /usr/bin/perl ./action.pl
pooja 10915 10890 0 16:43 pts/0 00:00:00 sh -c ps -ef| grep action.pl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

2. Shell Programming and Scripting

Need output of script on screen and file with correct return status of the called script.

Hi, I am trying to capture logs of the script in the file as well as on the screen. I have used exec and tee command for this. While using exec command I am getting the correct output in the file but, script output is not getting displayed on the screen as it get executed. Below is my sample... (14 Replies)
Discussion started by: Prathmesh
14 Replies

3. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

4. Shell Programming and Scripting

Shell script not getting called through cron job but executes fine manually.

Hi, My shell script not getting called through cron job. The same works fine when executed manually. I tried to generate logs to find if the scripts has some errors related to path using following command- trying to execute .sh file every 5 mins: */5 * * * * /home/myfolder/abc.sh... (17 Replies)
Discussion started by: Dejavu20
17 Replies

5. UNIX for Dummies Questions & Answers

Interpreting Shell Script errors when called from CRON

Hi All, I am calling a series of shell scripts via CRON so everything is running as root. However, in my error log file I am seeing the following errors. Please can anyone offer any advise as to the possible causes and solution to prevent the errors from appearing. The Error 1227 seems to... (2 Replies)
Discussion started by: daveu7
2 Replies

6. Linux

Why service (start|stop) can not be called from cron?

Hi, When I googled, I came to know that I can not call the command "service <service-name> start" from cron, insted I have to specify path to the service-name. I wanted to know the reason. Thanks, Hansini (3 Replies)
Discussion started by: hansini
3 Replies

7. Shell Programming and Scripting

Date command does not work in a KSH when called from Cron

Hi, I'm trying to execute a job on the last day of every month, for which i'm using the following code in my Korn shell script : if ]; then echo allowed only on last day of month. Today is `date +%d` >t.stm echo Tomorrow is `date +%d -d tomorrow` >s.stm exit 0 fi ... ....... (7 Replies)
Discussion started by: devilsadvocate
7 Replies

8. Shell Programming and Scripting

Script not working when called by cron

Hello, I have the following script which works fine when ran from the command line: #!/apps/python/2.3.4/bin/python import os import sys import time user = os.getenv("USER") string = time.strftime("%m%d%y0000 " + user, time.gmtime()) However, when I have this run by crontab, I... (4 Replies)
Discussion started by: cooldude
4 Replies

9. Shell Programming and Scripting

How to determine if a script (perl) was called from a CRON job or commandline

Hi, Is there a way to determine if a Script is called from a CRON job or from a commandline Gerry. (2 Replies)
Discussion started by: jerryMcguire
2 Replies

10. Shell Programming and Scripting

gzip in shell script called by cron

I'm puzzled by this one. I hope you can explain it to me. I have a ksh shell script that gzips a file among other things. This works perfectly fine when the script is manually run through a shell. However, when the same script is run through cron, it does everything correctly, but it will... (2 Replies)
Discussion started by: hbau419
2 Replies
Login or Register to Ask a Question