Running .sh every 5 minutes

35,721

Solution 1

The lazy option

If you want the easy way, and avoid having to find out which environment variable to set:

  • Make sure your script includes the shebang
  • Make it executable
  • Add the following to Startup Applications:

    /bin/bash -c "sleep 15 && while true; do <path_to_your_script.sh> ; sleep 300; done"
    

    Dash > Startup Applications > Add. Add the command:

    /bin/bash -c "sleep 15 && while true; do <path_to_your_script.sh> ; sleep 300; done"
    

Explanation

If you run the script from your own environment (e.g. from a terminal window or from Startup Applications), a number of environment variables will be set. cron however runs your script with a limited set of environment variables.
Since your script no doubt uses the gsettings command:

gsettings get org.gnome.desktop.background picture-uri <wallpaper>

to set the wallpaper, almost certainly the command will break when run from cron.

The downside of "the lazy solution" is purely theoretical. A command that sleeps practically continuously means nothing to your system.

Additional info; alternatively

Reading this post, and from experiences in the past, I am pretty sure the DBUS_SESSION_BUS_ADDRESS environment variable needs to be set.

To do so, add the following section at the beginning of your script (below the shebang):

PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

Then you should be able to successfully run it from cron.

You could however very well save the hustle, and choose the lazy option.

Solution 2

Put it in your crontab.

Open your cron table by:

crontab -e

Now add:

*/5 * * * * /path/to/script.sh

Don't forget to make the script executable beforehand.


As your script depends on X, probably will be a good idea to add the DISPLAY to the script's environment:

*/5 * * * * DISPLAY=:0 /path/to/script.sh

Replace :0 with your actual DISPLAY (can be found by echo $DISPLAY from interactive session).

If needed, you can add the XAUTHORITY environment variable too:

*/5 * * * * DISPLAY=:0 XAUTHORITY="~/.Xauthority" /path/to/script.sh

Again you can find the value by echo $XAUTHORITY.

Share:
35,721

Related videos on Youtube

Aleksander Mahnyov
Author by

Aleksander Mahnyov

Updated on September 18, 2022

Comments

  • Aleksander Mahnyov
    Aleksander Mahnyov almost 2 years

    I have a .sh script that downloads a photo from Japanese satellite's server and sets it as a background picture. I've put it into startup list, but how do I run it every, like, 5 minutes, automatically?

    • We are Borg
      We are Borg over 8 years
      Try out cronmaker.com , there you can make cron-expressions and put it with crontab -e
    • til_b
      til_b over 8 years
      Does the picture even change every 5 minutes? Try using an interval that creates less unnecessary load on the target server. Or make sure you only download if the file is really changed.
    • bohdan_trotsenko
      bohdan_trotsenko over 8 years
      By the way, what's the url for the space backgrounds?
  • heemayl
    heemayl over 8 years
    @JacobVlijm If any additional variable is needed, OP can define those in the script, no neeed for cron..
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    Absolutely, but it can be tricky, and not so straightforward to set up (find out) sometimes.
  • Aleksander Mahnyov
    Aleksander Mahnyov over 8 years
    Hm, it does not seem to work, while the script itself works, if I try to run it manually.
  • heemayl
    heemayl over 8 years
    @AleksanderMahnyov Please edit your question to add the script, hard to say without seeing the contents..possibly a DISPLAY (and/or XAUTHORITY) issue..
  • heemayl
    heemayl over 8 years
    @AleksanderMahnyov For starter, do one thing: make the cron entry as */5 * * * * DISPLAY=:0 /path/to/script.sh Replace :0 with your actual display found from echo $DISPLAY
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @AleksanderMahnyov You' re welcome. Glad it works :)
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @heemayl That won't do, I am pretty sure the DBUS_SESSION_BUS_ADDRESS environment variable needs to be set when editing gsettings.
  • heemayl
    heemayl over 8 years
    @JacobVlijm hmmm..hard to get to a conclusion without seeing the contents..
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    Ah, fun, a downvote. Care to share why?
  • Luka Ramishvili
    Luka Ramishvili over 8 years
    It's so much better to use a cron job.
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @LukaRamishvili Thanks for the comment. However: a. And why is that? a sleeping command means nothing to your system. b. I even included the option.
  • Luka Ramishvili
    Luka Ramishvili over 8 years
    I didn't see the mention of cron. As your answer is the accepted one, it would be better to add instructions for cron. There are multiple reasons why you'd prefer using cron; I can give you a few - that's the standard way for scheduled tasks; if the system restarts, it will still continue to run; crashing once won't affect subsequent runs (you won't have to check if it's still running); it already has features you may otherwise end up re-implementing, e.g. running on the same time every day, running as a different user, etc.
  • Luka Ramishvili
    Luka Ramishvili over 8 years
    @JacobVlijm a. you can post an update with attribution and link to the other answer, pretty standard b. every user has her own cron, and I didn't mention any other users c. I don't see 'no multiple runs' as a benefit, both ways impose minimal tax on the system.
  • Luka Ramishvili
    Luka Ramishvili over 8 years
    Otherwise, nice research about the gnome session variables, it would help the OP a lot, since that would be required either way.
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    I Just looked: cpu 0.0% What tax are we talking about?
  • heemayl
    heemayl over 8 years
    @Kartagis Cron can not handle seconds, also you are trying to do for hours which is still syntactically wrong..
  • Tolga Ozses
    Tolga Ozses over 8 years
    Oops, you are right, sorry
  • ZaxLofful
    ZaxLofful almost 7 years
    I love this method rather than the "lazy method", because I knew about cron, but needed a bit of a refresher course. This is exactly what I did, thanks a lot! :)
  • Sandeep Kumar
    Sandeep Kumar about 6 years
    Better approach than the accepted answer. :)