Operating System - HP-UX
1836042 Members
2612 Online
110089 Solutions
New Discussion

How to tell whether a script is run by cron or manually

 
SOLVED
Go to solution
Ridzuan Zakaria
Frequent Advisor

How to tell whether a script is run by cron or manually

I am writing a script that will perform that will send a notification to about the status of databases.

if it is run manually the script should send e-mail to database support group and if it is run by cronjob it should send PAGER notification to database support group.

How do it check whether the is run by cron or manually. Is there any process type that I can check?

Any help is greatly appreciated.

Thanks
quest for perfections
8 REPLIES 8
Christopher McCray_1
Honored Contributor

Re: How to tell whether a script is run by cron or manually

from root, type crontab -l and review the output for the script you are searching. if there, you should be good; if not enter it in by doing a crontab -e and edit it like you would a file through vi.

Good luck

Chris
It wasn't me!!!!
Magdi KAMAL
Respected Contributor
Solution

Re: How to tell whether a script is run by cron or manually

Hi Zakaria,

crontab jobs do not have attached tty ( that's why any standard output or standard error are sent to the mail ).

Any interactive process got an attached tty.

if you issue the command :

#ps -ef ? more

You will find the TTY under in the six column.

So, integrate in your script the following test :

WHICH_PS=`ps -ef ? grep ? grep -v grep ? awk '{ print $6 }' `

if [[ "$WHICH_PS" = "?" ]]
then
... it's a cron job execution ...
else
... it's a manual execution
fi


Good luck.

Magdi
Magdi KAMAL
Respected Contributor

Re: How to tell whether a script is run by cron or manually

Hi again,

in my last post stands for the script name.

Magdi
Ridzuan Zakaria
Frequent Advisor

Re: How to tell whether a script is run by cron or manually

Hi,

I think I should re-phrase my questions. I want to write a script that I have a capability to check itself whether it was run by cron or manually. If it was run by cront than I will perform certain task and if it was run manually it should do something else.

My goal it is to have a script that have dual usage manual or cron.

Thanks
quest for perfections
Ridzuan Zakaria
Frequent Advisor

Re: How to tell whether a script is run by cron or manually

Thanks Kamal,

This is what I am looking for.

quest for perfections
Darrell Allen
Honored Contributor

Re: How to tell whether a script is run by cron or manually

I see I'm too late but you could also do something like this:

VAR=`tty`
if [ "$VAR" = "not a tty" ]
then
echo Not running from a tty session
else
echo Running from tty $VAR
fi

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Magdi KAMAL
Respected Contributor

Re: How to tell whether a script is run by cron or manually

Hi again Zakaria,

if you integrate this solution and you plan to run the script manually, you do not need to run it with no hang up like :

#nohup myScript.sh

Because no hang up will not generate an attached tty for the myScript.sh process !

( I'm sure that you will not do it : " nohup ", but in order that the reply be complete ).

Nice time.

Magdi
Ridzuan Zakaria
Frequent Advisor

Re: How to tell whether a script is run by cron or manually

All,

Thanks for your help.
quest for perfections