Visit Our UNIX and Linux User Community


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?
# 1  
Old 06-14-2005
How to determine the script is called from CRON?

Hello expert,

What I want is to determine whether the script is called from CRON or it is executed interactively?

I tried the following but no luck:

#!/bin/ksh

cronID=`pgrep -x cron`
GPID=`ps -ef -o ppid,pid | grep " $PPID$" | awk '{print $1}'`

if [ "$cronID" == "$GPID" ]; then
echo I am being run through cron
else
echo I am NOT being run through cron
fi
# 2  
Old 06-14-2005
There can be a lot of processes named cron. You are picking one at random and attempting to see if it's your parent. In your crontab entry do something that you won't do interactively. Like:
55 18 * * * /path/to/script runfromcron
Then just check $1.

Code:
if [[ $1 = runfromcron ]] ; then
      echo running from cron
      shift
else
      echo not running from cron
fi

This faster, easier, and you have a way to more easily test the script's operation.
# 3  
Old 06-14-2005
How about checking those LOGIN specific environment variables from within the script? These variables are (check man page of the shell) such as $TERM, $PS1, etc.

Tom
# 4  
Old 06-15-2005
Quote:
Originally Posted by Perderabo
There can be a lot of processes named cron.
While agreeing with the rest of your post on this (minor) issue I'd like to disagree: that depends on the system you're running on. AIX, for instance, has cron started in /etc/inittab with the "respawn" option and hence there is no need to start more than one cron processes. In my career as AIX administrator I have never seen mor than one cron process active (or any number of cron instances save 1, for that matter).

bakunin
# 5  
Old 06-15-2005
crontab -e and look for the script.
# 6  
Old 06-15-2005
I guess the real question is you want to
know whether your script is running interactively or not?
I do this for bash scripts:

Code:
if ! tty > /dev/null; then
    echo "running non interactively"
fi

# 7  
Old 06-15-2005
Quote:
Originally Posted by bakunin
While agreeing with the rest of your post on this (minor) issue I'd like to disagree: that depends on the system you're running on. AIX, for instance, has cron started in /etc/inittab with the "respawn" option and hence there is no need to start more than one cron processes. In my career as AIX administrator I have never seen mor than one cron process active (or any number of cron instances save 1, for that matter).
How often have you looked for multiple cron processes? Would you notice a second cron if you simply ran a "ps -ef" but were looking for something else? I can't speak to AIX since I've never worked on it. But on other systems, cron runs programs by forking itself, fiddling with the environment, and then exec'ing the program to be run. After the fork, and prior to the exec you have a 2nd process named cron. Most people tend to not schedule jobs to be run at, say, exactly at 4 minutes after midnight. Instead everyone and his brother will tend to schedule stuff,say, exactly at midnight. At these busy points, you can have dozens of cron processes waiting to exec. I have often seen several cron processes on both HP-UX and Solaris. In addition, we have a monitoring system called Big Brother that used to check for for exactly one copy of cron running. I got dragged out of bed at 2 in the morning because cron forked and Big Bro freaked. Big Bro has now been corrected.

Also I do not really approve of running "ps -ef" and looking for processing by name. Users can write their own programs and they can call one of their own programs "cron". We just had a case where a developer wrote a program to be invoked from cron. And yes, he called the program to be invoked: cron. In the case of pgrep, "pgrep -u root -x cron" would at least avoid that, but the forking problem remains.

Previous Thread | Next Thread
Test Your Knowledge in Computers #943
Difficulty: Medium
The Smurf attack is a DDOS attack in which large numbers of Internet Control Message Protocol (ICMP) packets with the intended victim's spoofed source IP are broadcast to a computer network using an IP broadcast address.
True or False?

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

Featured Tech Videos