4

I have this bash script:

while [[ 1 ]] ; do sleep 3600 ; ./notify.sh --text "ricordati di bere" && play /mnt/musica/login.wav && zenity --info --text="<span size=\"xx-large\">Time is $(date +%Hh%M).</span>\n\nricordati di <b>bere</b>." --title="drink time" ; done  

I'd like to execute this script (from 8:00 o'clock, less important) to 19:00 (most important); it's possible?
I have seen at command, but I didn't find how set "until" or "before".

This question is different from this

1
  • Monday through Friday or 7 days a week? What if the script fired regularly but only notified between those hours?
    – Jeff Schaller
    May 7, 2019 at 16:28

2 Answers 2

4

You would do the scheduling with cron. The schedule would look like

0 8-19 * * * /path/to/script

or

0 8,9,10,11,12,13,14,15,16,17,18,19 * * * /path/to/script

and the script would look like

#!/bin/sh

./notify.sh --text "ricordati di bere" &&
play /mnt/musica/login.wav &&
zenity --info \
    --text="<span size=\"xx-large\">Time is $(date +%Hh%M)</span>\n\nricordati di <b>bere</b>." \
    --title="drink time"

See also "How to send a mail for every 10 minutes through shell script?"

6
  • @Duns If you added the cron job as root, then it's added to root's crontab. You would view root's crontab with sudo crontab -l. However, I just realised that you are trying to use GUI applications, so using cron for this may not be the easiest solution. Cron jobs have no connection to the display and can't generally open dialog boxes or windows.
    – Kusalananda
    May 7, 2019 at 19:38
  • thanks. It seems work, somehow (maybe not perfectly; btw I dont' use a GUI): at 8:00 I received on my smartphone the notify, when I was not at home; but coming back to home I didn't see the pop-up. So I tried double-click on the script and it works perfectly. I will see at 9:00. Another thing: if I would do from 8:30 to 11:30 and from 15:30 to 18:30, it should possible with cron?
    – Duns
    May 8, 2019 at 6:40
  • @Duns See man 5 crontab. 30 8-11,15-18 * * * /path/to/script
    – Kusalananda
    May 8, 2019 at 6:57
  • Thank you very much. It works, but only the sound and the smartphone notify, not the PC pop-up. I started a new session. I will see.
    – Duns
    May 8, 2019 at 7:13
  • 1
    It works: I added "--display=:0.0" to zenity command in bash script (as said here). Not what you said, even you gave me a great help. Thank you very much
    – Duns
    May 8, 2019 at 8:34
1

Presuming you use Linux, timeout is the simplest way to do this. 08:00 to 19:00 is 11 hours, so we tell timeout to run the script for 11 hours.

timeout 11h /home/username/script

Start the job with cron at 08:00 (or manually whenever you wish) and have it killed automatically at 19:00 sharp with another cron entry

kill -9 /home/username/script
2
  • Thanks, but I start the Pc non always at the same time, so your solution is not what I search for.
    – Duns
    May 7, 2019 at 18:40
  • See rephrasing above which shows greater flexibility.
    – K7AAY
    May 7, 2019 at 18:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .