LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-20-2011, 08:30 AM   #1
Fracker
Member
 
Registered: Mar 2009
Posts: 86

Rep: Reputation: 0
script to know whether croned or manuall executed


Hi guyz,

How to find whether the script executed manually or executed from cron job?

I tried $SHELL but this don't work
 
Old 05-20-2011, 08:40 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Jobs that are started by cron should be mentioned in the cron log file(s).

Have a look in /var/log or /var/log/cron.

Hope this helps.
 
1 members found this post helpful.
Old 05-20-2011, 09:19 AM   #3
Fracker
Member
 
Registered: Mar 2009
Posts: 86

Original Poster
Rep: Reputation: 0
You mean like this ?

Code:
#!/bin/bash

last_cron_time=$( cat /var/log/cron | grep "sh /root/c.sh" | cut -c 8- | tail -n 1 | cut -c -5)

HH=$(date +"%H")
MM=$(date +"%M")
HH1=${last_cron_time:0:2}
MM1=${last_cron_time:3:2}

if [ $HH -eq $HH1 -a $MM -eq $MM1 ]; then
   echo "I am croned"
else
   echo "I am manual"
fi
Looks quite complex to me .... But it's working

Last edited by Fracker; 05-20-2011 at 09:20 AM.
 
Old 05-20-2011, 09:45 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

This part can be shortened:
Code:
last_cron_time=$( cat /var/log/cron | grep "sh /root/c.sh" | cut -c 8- | tail -n 1 | cut -c -5)
I'm not sure if the output of your cron log is the same as mine, but this works on my side:
Code:
last_cron_time=$( awk '/sh \/root\/c.sh/ { print $3 }' /var/log/cron | tail -n 1)
(forward slashes do need to be escaped).

Hope this helps.
 
1 members found this post helpful.
Old 05-20-2011, 10:36 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Fracker View Post
Hi guyz,

How to find whether the script executed manually or executed from cron job?
If "executed manually" means run from a terminal, the "standard" test is to check $PS1 but this test is more reliable:
Code:
case "$( /bin/ps -p $$ -o tty 2>&1 )" in
    *TT*-* | *TT*\?* ) 
        <script is not running from a terminal; could be by cron>
        ;;  
    *TT* )
        <script is running from a terminal>
esac
 
1 members found this post helpful.
Old 05-20-2011, 12:29 PM   #6
Fracker
Member
 
Registered: Mar 2009
Posts: 86

Original Poster
Rep: Reputation: 0
wow cool thanks $PS1 wasn't working for me, this works like a charm just have tested

Code:
case "$( /bin/ps -p $$ -o tty 2>&1 )" in
    *TT*-* | *TT*\?* ) 
        <script is not running from a terminal; could be by cron>
        ;;  
    *TT* )
        <script is running from a terminal>
esac
 
  


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
When was the script last executed? AnAsianCalledRusty Linux - Newbie 1 01-18-2011 11:21 AM
Which script gets executed? junglepunk Linux - Newbie 20 06-18-2010 09:44 AM
Shell script executed on login kefi Linux - Desktop 13 03-01-2010 11:04 AM
Getting location of the script which is currently being executed jiju.jacob Linux - Newbie 4 07-21-2008 06:48 AM
script executed inside another script dont make changes potraike Linux - Newbie 4 03-05-2008 06:27 AM


All times are GMT -5. The time now is 08:00 PM.

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