Skip to content

Instantly share code, notes, and snippets.

@sshymko
Last active January 29, 2020 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sshymko/0f2e1c6251fa69584d94c43e549f164b to your computer and use it in GitHub Desktop.
Save sshymko/0f2e1c6251fa69584d94c43e549f164b to your computer and use it in GitHub Desktop.
Wrapper launching single process using its execution permission as concurrency semaphore
#!/bin/sh
if [ -z "$1" ]; then
echo "Usage: $0 <command> [args...]"
exit
fi
trap ':' SIGINT SIGTERM
chmod u-x "$0"
"$@"
status=$?
chmod u+x "$0"
exit $status
@sshymko
Copy link
Author

sshymko commented Oct 30, 2019

Protect cron from launching new jobs until the previous one has finished:

*/10 * * * * /path/to/only.sh sleep 900

Even though the job is scheduled to run every 10 min, it will be executed every 20 min, because it takes 15 min (900 sec) to complete and every other launch attempt will fail with the Permission denied error protecting from undesirable concurrency.

Note: Every job needs its own copy of only.sh as the script's execution permission bit is used as a semaphore; symlink won't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment