LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Test if script is launched from cron or CL (https://www.linuxquestions.org/questions/programming-9/test-if-script-is-launched-from-cron-or-cl-639409/)

0x29a 05-02-2008 12:29 PM

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

unSpawn 05-02-2008 01:20 PM

Quote:

Originally Posted by 0x29a (Post 3140161)
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?

0x29a 05-02-2008 01:33 PM

Quote:

Originally Posted by unSpawn (Post 3140207)
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

chrism01 05-05-2008 06:28 PM

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)

0x29a 05-06-2008 12:35 AM

Quote:

Originally Posted by chrism01 (Post 3143932)
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


All times are GMT -5. The time now is 07:58 PM.