How to check if script is run via cron or manual=command line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check if script is run via cron or manual=command line?
# 1  
Old 09-01-2012
How to check if script is run via cron or manual=command line?

Hi all,

I have a script that can be run via cron or via the command line.

Is there any way that I can place something on the script to be able to distinguish/differentiate whether the script was run via a user in the command line or whether it was run from the cron?
# 2  
Old 09-01-2012
# 3  
Old 09-01-2012
Please remember to post what Operating System and version you are running and what Shell you use.

Ignoring the link from @mjf, the simple answer for most Bourne-type Shells is to test whether SDOUT is a terminal or not:

Code:
if [ -t 1 ]
then
        echo "From cron"
else
        echo "Not from cron"
fi

# 4  
Old 09-02-2012
@methyl: You've typed it the other way around:
Code:
if [ -t 1 ] ; then 
   echo "Not from cron"  #since FD 1 (stdout) is open
else
   echo "From cron"   
fi

Note that this may not be reliable for all cases. E.g. In your script you redirect all output to a file; then stdout is not open, and [ -t 1 ] will evaluate to false.

I personally like Perderabo's solution from the link above best.

Last edited by mirni; 09-02-2012 at 02:35 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Run script as root from command line

I have a script (ksh) that has permissions 775 and owned by root.system. This script takes the parameter of a full file name and chmods the file to 666 and changes ownership to user smith.staff. ex: modify_file.ksh /home/smith/filea modify_file.ksh has 775 and root.system ownership. The... (1 Reply)
Discussion started by: mlacriola
1 Replies

2. AIX

Commands to call script work from command line but not from Cron entry

My first post evidently did not materialize so I posted it again: Runnning a cron job every 5 mins to send data files to a state facility. My original cron entry at worked fine: 01,06,11,16,21,26,31,36,41,46,51,56 * * * * /home/sftpuser/stateinoc-from-appname.ksh Somewhere I have a... (1 Reply)
Discussion started by: Skyybugg
1 Replies

3. Shell Programming and Scripting

Script runs in command-line fine but times out in CRON?

Hi, I have a script that seems to run to completion when in the command-line, but when it is run using the cron, it seems to time out. They both start and run fine, but on the CRON it stops prematurely. The script hits an API every few seconds and grabs data. Does anyone have any idea on... (4 Replies)
Discussion started by: phpchick
4 Replies

4. Shell Programming and Scripting

CRON shell script only runs correctly on command line

Hi, I'm new to these forums, and I'm hoping that someone can solve this problem... To make things short: I have DD-wrt set up on a router. I'm trying to run a script in CRON that fetches the daily password from my database using SSH. CRON is set like so(in web interface): * * * *... (4 Replies)
Discussion started by: louieaw
4 Replies

5. Shell Programming and Scripting

Run perl script, with command-line options

Hello everyone, I have a perl script which takes various command line options from user like : test.pl -i <input_file> -o <output_file> -d <value> -c <value> Now I have multiple input files in a directory: <input_file_1> <input_file_2> <input_file_3> <input_file_4> ..... .... ...... (6 Replies)
Discussion started by: ad23
6 Replies

6. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

7. UNIX for Dummies Questions & Answers

Manual run a script on UNIX

Hi,I'm currently working on an dev environment that has no jobs to run the scripts.How can I manual run the scripts. on Unix? ---------- Post updated at 05:45 AM ---------- Previous update was at 05:43 AM ---------- Is it something like this.... ../int/inbound>./filename.sh inputfilename ... (5 Replies)
Discussion started by: sonja
5 Replies

8. UNIX for Dummies Questions & Answers

Script through cron and command line

I have a script which runs fine through command line, but doesn't run through cron. There are some variables which are set by the .profile file which are used by the script. Is it that cront does not pick these variables. $/export/home/rahul/bin/createfile.sh >>... (3 Replies)
Discussion started by: rahulrathod
3 Replies

9. UNIX for Dummies Questions & Answers

script works on command line, not in cron job

Hey there, I'm a total newbie unix guy here and just picking this stuff up. Have a very small script I put together that works fine from the command line but not once I put it in a cron job. Searched and found this thread and am wondering it it has something to do with setting variables, though the... (7 Replies)
Discussion started by: JackTheTripper
7 Replies

10. Shell Programming and Scripting

script execute by cron problem, but manual ok

Hi; I'm facing the problem with my script like below 30 0 * * * /data/SCRIPT/LOADLOGS/loadata1.sh 2 1 >> /tmp/loaddata.txt loadata1.sh calling a another 2 scripts which we need to exe in sequence. when it perform by cron jon, sometimes it just can process 20 recs or less or nothing but... (6 Replies)
Discussion started by: izai
6 Replies
Login or Register to Ask a Question