Cron: Difference between revisions

From miki
Jump to navigation Jump to search
Line 33: Line 33:


. "$1"
. "$1"
setsid /usr/bin/env -i "$SHELL" -c "set -a; . $1; $2" </dev/null
exec setsid /usr/bin/env -i "$SHELL" -c "set -a; . $1; $2" </dev/null
</source>
</source>



Revision as of 00:19, 5 June 2017

Tips

Run a cron job with random delay

From SO:

0 1 * * * perl -le 'sleep rand 9000' && *command goes here*

Test script as if run by cron

From SO.

Add cron entry:

 * * * * *  /usr/bin/env > /home/username/cron-env

Create a script run-as-cron:

#!/bin/sh
#
# https://stackoverflow.com/questions/8132355/test-run-cron-entry
#
# Add cron entry:
#
#   * * * * *  /usr/bin/env > /home/username/cron-env
#
# Then:
#
#   run-as-cron <cron-environment> <command>
#
# for instance
#
#   run-as-cron /home/username/cron-env 'echo $PATH'

. "$1"
exec setsid /usr/bin/env -i "$SHELL" -c "set -a; . $1; $2" </dev/null

Now to test script as if run by cron:

run-as-cron ~/cron-env 'echo $PATH'
run-as-cron ~/cron-env ~/somescript.sh