0

My goal is to start a task (specifically, a package to run in Linux Mint 21) at startup between two certain times of day, or at the start time if the computer is already running, to stop at the end time. I know how to accomplish 2 of the three tasks, run at startup and run at certain tod in a cron script, but I dont know how to combine all three statements in a bash script to be referenced in a cron job.

Thank you

I have looked up various solutions on the internet, including pages detailing if and while statements in bash scripts, and various options in cron. None have been specific enough.

I expect my bash script to meet the above criteria and to be inserted into a cron file.

Una Mnt
  • 1
  • 1
  • Please don't add code in comments. Edit your question instead – Aserre Nov 22 '22 at 16:16
  • Also : 1) this code wont work with `[`. Use `[[` instead to chain conditions. 2) `>=` is not understood by bash. See [this question](https://stackoverflow.com/questions/18668556/how-can-i-compare-numbers-in-bash) for instance. You can also use [shellcheck](https://www.shellcheck.net/) to check for other syntax errors – Aserre Nov 22 '22 at 16:27

1 Answers1

0

"at startup" and "in a cron job" aren't really good to combine.

In your "at startup" script, just check the time. Assuming you mean something like "if the current time is 8am to 5pm", then I'd do something like this -

Running these at 11am.

For the hours of 8 to 10 AM,

$: declare -A span=();
$: for hr in {08..10}; do span[$hr]=1; done
$: ((${span[$(date +%H)]})) && echo do the thing || echo not doing the thing now
not doing the thing now

For 8AM to 5PM,

$: declare -A span=();                      # make a lookup tab;le
$: for hr in {08..16}; do span[$hr]=1; done # populate it
$: ((${span[$(date +%H)]})) && echo do the thing || echo not doing the thing now
do the thing

You could also do it as

$: now=$(date +%H); (( now >= 10#08 && now <= 10 )) && echo doing || echo not doing
not doing

$: now=$(date +%H); (( now >= 10#08 && now <= 16 )) && echo doing || echo not doing
doing

(The 10#08 in that is to make sure the two-digit numbers with leading zeros are still parsed as base 10 instead of octal.)

Or you could use case statements.

$: case $(date +%H) in 08|09|10 ) echo doing;; *) echo not doing;; esac

or even (if your version is last enough)

$: case $(date +%H) in  
   08)                ;& 
   09)                ;&
   10)                ;&
   11)                ;&
   12)                ;& 
   13)                ;&
   14)                ;& 
   15)                ;&
   16) echo doing     ;; # fall through with ;& lines in a case statement
    *) echo NOT doing ;; esac
doing
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • I got the script to execute at startup between certain times of day, but how do I execute and kill it at the start and end times without putting it in a cron job? – Una Mnt Nov 23 '22 at 05:07
  • You may well execute the script in a cron job at start time, and kill it in a cron job at end time. – Armali Nov 23 '22 at 10:32
  • I misunderstood - I thought you wanted to run once, either the machine booted up - or maybe when someone logged in. If you're running it periodically, cron is perfect. If only at start and end, and your startup and shutdowns call the service scripts, there would be a good place. Maybe [this](https://phoenixnap.com/kb/crontab-reboot) is what you needed? – Paul Hodges Nov 27 '22 at 17:36