Skip to content

Instantly share code, notes, and snippets.

@commenthol
Created October 18, 2015 19:14
Show Gist options
  • Save commenthol/c50bd13901fdc6da2f9f to your computer and use it in GitHub Desktop.
Save commenthol/c50bd13901fdc6da2f9f to your computer and use it in GitHub Desktop.
a cron job for non root
#!/bin/bash
# add the times you'd like to schedule fcmd
times=(8:00 13:00 23:00)
# your commands
fcmd() {
# add your commands here
echo "execute your commands here"
}
# now date
fnow() {
echo $(date +%s)
}
# calculates the seconds till next schedule
fnext_sleep() {
local now=$(fnow)
local next=100000
for time in ${times[@]} ; do
local nexttimes=($(date +%s -d "$time") $(date +%s -d "$time 1 day"))
for nexttime in ${nexttimes[@]} ; do
local d=$(expr $nexttime - $now)
if [ $d -gt 0 ] ; then
if [ $d -lt $next ]; then
next=$d
fi
fi
done
done
echo $next
}
# our endless loop
while : ; do
fcmd
next=$(fnext_sleep)
time=$(date +"%Y-%m-%d %H:%M" -d @$(expr $(fnow) + $next))
echo ... sleeping $next seconds until $time
sleep $next
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment