Last active
November 6, 2023 09:42
-
-
Save daladim/7f67eb95d59aadc8f3e8cd66c6f235d3 to your computer and use it in GitHub Desktop.
Run a command or a script as cron would
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Run as if it was called from cron, that is to say: | |
# * with a modified environment | |
# * with a specific shell, which may or may not be bash | |
# * without an attached input terminal | |
# * in a non-interactive shell | |
# This scripts supports cron jobs run by any user, just run it as the target user (e.g. using sudo -u <username>) | |
# An up-to-date version of this script may be available at https://github.com/daladim/run-as-cron | |
function usage(){ | |
echo "$0 - Run a script or a command as it would be in a cron job, then display its output" | |
echo "Usage:" | |
echo " $0 [command | script]" | |
echo "" | |
echo "This scripts supports cron jobs run by any user." | |
echo "To do so, just run it as the target user (e.g. using sudo -u <username> $0)" | |
} | |
if [ "$#" -lt 1 -o "$1" == "-h" -o "$1" == "--help" ]; then | |
usage | |
exit 0 | |
fi | |
# Depending on the distro, $HOME may or may not be affected by sudo. | |
# This is a better way to get the home directory of the current user | |
home=$( getent passwd "$(whoami)" | cut -d: -f6 ) | |
cron_env="$home/.config/run-as-cron/cron-env" | |
function generate_env_file(){ | |
# This function adds /usr/bin/env to a cron job and makes sure it is run only once | |
# i.e. it just helps the user who does not want to do it himself | |
echo -n "Adding a job to cron, to be run once... " | |
generator=$(mktemp /tmp/generator.cron.XXXX) | |
chmod 700 "$generator" | |
cat > "$generator" <<eof | |
#!/bin/bash | |
mkdir -p $(dirname "$cron_env") | |
/usr/bin/env > ${cron_env} | |
# Remove this script from the current cron jobs | |
crontab -l | sed "/$(basename $generator)/d" | crontab - | |
rm "$generator" | |
eof | |
# Add the just-created-script to cron | |
crontab -l | { cat; echo "* * * * * $generator"; } | crontab - | |
if [ "$?" -eq 0 ]; then | |
echo "Done at $(date)" | |
echo "It will be run shortly. You can try to run $0 again in a minute." | |
return 0 | |
else | |
echo "Failed." | |
return 2 | |
fi | |
} | |
# This file should contain the cron environment. | |
if [ ! -f "$cron_env" ]; then | |
echo "Unable to find $cron_env" >&2 | |
echo "To generate it, run \"/usr/bin/env > $cron_env\" as a cron job" >&2 | |
echo -n "Do you want this script to do it for you? [y/n] " | |
read reply | |
if [ "$reply" == "y" -o "$reply" == "Y" ]; then | |
generate_env_file | |
exit $? | |
fi | |
exit 1 | |
fi | |
# It will be a nightmare to expand "$@" inside a shell -c argument. | |
# Let's rather generate a string where we manually expand-and-quote the arguments | |
# Note that may fail in case arguments (or environment variables) contain quotes... | |
env_string="/usr/bin/env -i " | |
while read -r line; do | |
env_string="${env_string} \"$line\" " | |
done < "$cron_env" | |
cmd_string="" | |
for arg in "$@"; do | |
cmd_string="${cmd_string} \"${arg}\" " | |
done | |
# Which shell should we use? | |
the_shell=$(grep -E "^SHELL=" "$cron_env" | sed 's/SHELL=//') | |
if [ -z "$the_shell" ]; then | |
echo "Unable to detect what shell should be used for this user." >&2 | |
exit 2 | |
fi | |
echo "Running with $the_shell the following command: $cmd_string" | |
# Let's route the output in a file | |
# and do not provide any input (so that the command is executed without an attached terminal) | |
so=$(mktemp "/tmp/fakecron.out.XXXX") | |
se=$(mktemp "/tmp/fakecron.err.XXXX") | |
"$the_shell" -c "$env_string $cmd_string" >"$so" 2>"$se" < /dev/null | |
echo -e "Done. Here is \033[1mstdout\033[0m:" | |
cat "$so" | |
echo -e "Done. Here is \033[1mstderr\033[0m:" | |
cat "$se" | |
rm "$so" "$se" |
I think this risk of a gist means they are never worth it. It's always better to put the code in a repo. :-)
I saw those comments and that's what prompted my comment here. I see no sense in two people integrating those comments. I'll see how much time I have to get those in.
If you'd like to own the repo I'd just as soon transfer ownership to you and just submit PRs. Would you like me to do that?
Well, I finally created https://github.com/daladim/run-as-cron, feel free to create MRs there as well!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for these improvements.
I only created a gist because I did not think I would have that many suggestions for improvement, and I did not think a fully-fledged repo would be useful :)
Also, someone recently commented for many improvements: https://unix.stackexchange.com/a/580656/330049
I'm not sure I'll have time to include them in the short run. If you do, then please keep your repo, and I'll happily deprecate my gist, and point to your better repo. Otherwise, I think I'll do this, but probably not before a couple of weeks...