Skip to content

Instantly share code, notes, and snippets.

@lwcorp
Created March 25, 2024 14:08
Show Gist options
  • Save lwcorp/be3bac252e3e8a82c4c5ad423d8acaf0 to your computer and use it in GitHub Desktop.
Save lwcorp/be3bac252e3e8a82c4c5ad423d8acaf0 to your computer and use it in GitHub Desktop.
Automatically add a command to cronjob to 1 minute later and remove once it starts (good for testing if a command runs well in a cronjob)
#!/bin/bash
# For example a Python command
user=type your user
folder=type your folder
command="source $HOME/.bashrc && source /home/$user/myenv/bin/activate && cd /home/$user/public_html/$folder && python index.py -run"
if [ "$1" = "-dummy" ]; then
command="$command $1"
elif [ "$2" = "-dummy" ]; then
command="$command $2"
fi
if [ "$1" = "-run" ]; then
eval $command
else
crontab -l > cron_orig
cp cron_orig cron_new
echo $(date -d "1 minute" +"%-M %-H %-d %m %-w") $command >> cron_new
crontab cron_new
rm cron_new
sleep 60
crontab cron_orig
rm cron_orig
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment