Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Run a command or a script as cron would
# 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
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
exit 0
# 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 )
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
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"
# 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
echo "Failed."
return 2
# 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
exit $?
exit 1
# 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"
for arg in "$@"; do
cmd_string="${cmd_string} \"${arg}\" "
# 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
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"
Copy link

poleguy commented Jul 13, 2020

This would be better if it would bootstrap the /usr/bin/env > /root/cron-env the first time it runs.

It also doesn't work if cron-env has a semicolor (';') in an environment variable, e.g.:

This doesn'tget escaped right.

bash sucks.

Copy link

daladim commented Jul 13, 2020

@poleguy, thanks for your input.
I have just added a feature that bootstraps /usr/bin/env > /root/.config/run-as-cron/cron-env, just as you suggested.

I can see that you are trying (on your own fork) to fix this script when the environment contains semicolons. Feel free to tell me when/if you successfully test it, I'd be happy to include your improvements in this script!

Copy link

daladim commented Jul 17, 2020

@poleguy, I now have included your improvements in this gist.
I also have improved a few other things, added better error management, and this script now supports any user.

Copy link

nbonamy commented Dec 13, 2021

Thanks for this! Updated the script: if no command is provided then crontab commands will be listed and user will be able to pick one. Available on

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