test if script called by cron

2013-09-16 Thread Paul Macdonald


Hi,

Is there a simple way of testing whether a given script was called via cron,

I'd rather find a solution that would work from within the script rather 
than setting an environment variable in the crontab.


thanks
Paul.

(anyone here going to EuroBSD con?)

--
-
Paul Macdonald
IFDNRG Ltd
Web and video hosting
-
t: 0131 5548070
m: 07970339546
e: p...@ifdnrg.com
w: http://www.ifdnrg.com
-
IFDNRG
40 Maritime Street
Edinburgh
EH6 6SA

High Specification Dedicated Servers from £100.00pm


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: test if script called by cron

2013-09-16 Thread Polytropon
On Mon, 16 Sep 2013 12:26:59 +0100, Paul Macdonald wrote:
 Is there a simple way of testing whether a given script was called via cron,
 
 I'd rather find a solution that would work from within the script rather 
 than setting an environment variable in the crontab.

I'd suggest the script creates a file (lock file or,
much easier, just a simple normal file) at its beginning:

#!/bin/sh
/usr/bin/touch /tmp/scriptrun
# ... your script content here ...

You could also output the date command to that file
to see when the script has been called:

#!/bin/sh
/bin/date +%Y-%m-%d %H:%M:%S  /tmp/scriptrun
# ... your script content here ...

Of course you would have to manually remove that file
after you have verified its existence and content.



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: test if script called by cron

2013-09-16 Thread Dan Nelson
In the last episode (Sep 16), Paul Macdonald said:
 Is there a simple way of testing whether a given script was called via cron,
 
 I'd rather find a solution that would work from within the script rather 
 than setting an environment variable in the crontab.

You check to see if stdin is a terminal, but that's not conclusive.  One way
to know for sure is to look at the name of the process that launched you:

if [ ! -t 0 ] ; then
 echo no tty, possibly run from cron
fi

parent=$(ps -o command= -p $PPID)
case $parent in 
 *cron* ) echo parent is $parent, almost certainly cron ;;
esac

-- 
Dan Nelson
dnel...@allantgroup.com
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: test if script called by cron

2013-09-16 Thread Jerry
On Mon, 16 Sep 2013 12:26:59 +0100
Paul Macdonald articulated:

 
 Hi,
 
 Is there a simple way of testing whether a given script was called
 via cron,
 
 I'd rather find a solution that would work from within the script
 rather than setting an environment variable in the crontab.
 
 thanks
 Paul.
 
 (anyone here going to EuroBSD con?)

If you want to learn if the running script was called via cron, this
would work, assuming you are running Bash.

if [[ ! -t 0 ]]; then
echo Running from Cron
fi

-- 
Jerry ♔

Disclaimer: off-list followups get on-list replies or get ignored.
Please do not ignore the Reply-To header.
__

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org

Re: test if script called by cron

2013-09-16 Thread Polytropon
On Mon, 16 Sep 2013 23:28:17 -0400, kpn...@pobox.com wrote:
 On Mon, Sep 16, 2013 at 02:05:04PM +0200, Polytropon wrote:
  On Mon, 16 Sep 2013 12:26:59 +0100, Paul Macdonald wrote:
   Is there a simple way of testing whether a given script was called via 
   cron,
   
   I'd rather find a solution that would work from within the script rather 
   than setting an environment variable in the crontab.
  
  I'd suggest the script creates a file (lock file or,
  much easier, just a simple normal file) at its beginning:
  
  #!/bin/sh
  /usr/bin/touch /tmp/scriptrun
  # ... your script content here ...
 
 Wouldn't the lockf command be better than touch? That way you get the
 condition code telling you whether or not the script is already running.

Yes, it would probably be better in this case. This, in
combination with the suggestion of test-t 0 to check
if the script has been interactively called or not, looks
like a better solution.

However, the intial question does not make fully sure (at
least to me as a non-native speaker) if the intention is
(a) to check _if_ the script has been run via cron, or
(b) to check if the script has been run via _cron_. :-)



  Of course you would have to manually remove that file
  after you have verified its existence and content.
 
 If you use lockf as a drop-in replacement for touch then, yes, you'll
 need to keep the lock file until removing it at the end of the script.

Depends. Let's say the script is scheduled at 3:00 and will
finish in about half an hour. The evidence file will only
be visible from 3:00 to ca. 3:30, so removing the evidence
file after the script has finished could lead to a false-negative
result (has not been run). This is also true for the more
simple solution using the touch command (no rm call at the
end of the script).



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org