The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to insert few lines in the called script suri Shell Programming and Scripting 7 03-06-2007 09:25 AM
Redirecting to standard output from within called script vigneshra Shell Programming and Scripting 8 07-04-2006 11:19 PM
How to determine if a script (perl) was called from a CRON job or commandline jerryMcguire Shell Programming and Scripting 2 03-23-2006 10:47 AM
Need help with ksh script that uses sqlplus, called from PHP j2owilson Shell Programming and Scripting 7 02-27-2006 04:35 PM
gzip in shell script called by cron hbau419 Shell Programming and Scripting 2 02-05-2002 07:03 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-14-2005
wes_brooks wes_brooks is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 1
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 (permalink)  
Old 06-14-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,123
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 (permalink)  
Old 06-14-2005
tom_xx_hu@yahoo tom_xx_hu@yahoo is offline
Registered User
  
 

Join Date: May 2005
Posts: 40
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 (permalink)  
Old 06-15-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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 (permalink)  
Old 06-15-2005
dnidiffer dnidiffer is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 4
crontab -e and look for the script.
  #6 (permalink)  
Old 06-15-2005
pixelbeat pixelbeat is offline
Registered User
  
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
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 (permalink)  
Old 06-15-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,123
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0