LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-02-2008, 12:29 PM   #1
0x29a
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Rep: Reputation: 0
Test if script is launched from cron or CL


Disclaimer: I, like many, am not a programmer, but I can grasp things with help.

I'm trying to develop yet another way to copy files from a server to external media. I want to be able to test whether it's running from cron or the command line. The command line test seems fairly obvious: If the script is run manually with no arguments then show usage. (I'll work out how to restrict this to a date format before I put it in production.)

Below is the first step that creates a directory structure to move the archive to. It's incomplete still but the idea is that it will create a date-based directory name. It seems the cron test needs to happen before the while loop.

Code:
#!/bin/bash
#

vflag=off
# Set DirDate1 from command line
DirDate1=$1
# Set DirDate2 for use from cron
DirDate2=$(date +'%Y-%m-%d')

# cron test here?

# Test whether external device is mounted and capture mount exit code
if /mnt/external/server_backups !exist  then
      mount /mnt/external
        ) test exit code to do some other stuff that I can't find my notes on
          and email my phone if mount fails.
fi
while [ "$DirDate1" = "" ] # test for arguments
do                         # echo usage
        case "$1" in
        "") echo >&2 \
            "
Creates backup directory structure with
date input as part of directory name, then
performs selective copy.

usage: $0 [yyyymmdd]
            "
            exit 1;;
        esac
        shift
done

# DirDate must be enclosed in double quotes
mkdir /mnt/external/server_backups/week_ending_"$DirDate"

# rsync stuff will go here when I get it figured out and tested
#

exit
So, how would you all suggest I test if the script is being launched from cron? It's probably right in front of my face, which is why I can't see it....

Thanks,
Andrew
 
Old 05-02-2008, 01:20 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,311
Blog Entries: 54

Rep: Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860Reputation: 2860
Quote:
Originally Posted by 0x29a View Post
how would you all suggest I test if the script is being launched from cron?
The PPID of this script is the PID of crond?
 
Old 05-02-2008, 01:33 PM   #3
0x29a
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by unSpawn View Post
The PPID of this script is the PID of crond?
Oow, good point! I'll look in to that. So, I'm testing the PPID of the script and that can change for whatever reason (reboot, crash, or whatever) and not affect the test. I think I can find some examples of a PPID test.

I will be testing for either cron or crond depending on distro implementation, apparently. SuSE and Ubuntu both use cron, and CentOS uses crond, for example.


Thanks for the clarity!

Andrew
 
Old 05-05-2008, 06:28 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
You can also use the -t test ( http://www.tldp.org/LDP/abs/html/fto.html ) to check if stdin/stdout is associated to a terminal (false if run from cron)
 
Old 05-06-2008, 12:35 AM   #5
0x29a
LQ Newbie
 
Registered: Jun 2004
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
You can also use the -t test ( http://www.tldp.org/LDP/abs/html/fto.html )
Thank you for the reply. I know I've seen something similar in startup scripts, so this is very helpful! I'll post the whole script when (read: if) I finish it.

Thanks again.

Andrew
 
  


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
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
cron 'test' message in my /var/log/messages file visaris Linux - Newbie 1 12-13-2004 04:03 PM
PPPD Script problem when cron-scheduling the script andresurzagasti Linux - Networking 0 11-24-2004 02:54 PM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 02:09 PM


All times are GMT -5. The time now is 07:32 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration