0

I've recently started a simple project, just to help me learn the BASH scripting language a little better. Anyway, this script is set to alternate/rotate the user's desktop background/wallpaper at a given interval.

Given that this task would most likely be done every 30 minutes to 1 hour, how should I go about doing this. Would 30 minute/1 hour timers be very inefficient? Or, could cronjobs do a better job for me?

Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?

Could you provide some sort of an idea into the syntax, if you can, as well.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user2298964
  • 275
  • 1
  • 3
  • 7

3 Answers3

1

This would be a suitable job for cron. cron would take care of invoking the script at regular intervals. You would not then have to be concerned in your script when the script should run and managing a script running in the background.

Running in the background would be extravagent as the script does not need to do much - not much more than change the current desktop setting. Typically the script would only take a small fraction of a second to complete the task.

cron entries have six fields-:

 mins hours day  month day-of-week path_to_command
 0-59 0-23  1-31 1-12      0-6      command
days of the week start on Sunday. 0=Sunday, 1=Monday etc.

cron entry to run the script every hour for all days and months-:

0 * * * * /path/change_wallpaper.sh

to list your current cron jobs, type

 crontab -l 

Edit your cron jobs and add the new cron entry-:

 crontab -e

Check the new setting is in place -:

 crontab -l
suspectus
  • 16,548
  • 8
  • 49
  • 57
0

Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?

That would be a daemon. And there's no need to write your own. It's a bit tedious in bash if you want pidfile, start|stop|restart etc. Just add a new cronjob which'll execute your script every n minute or something.

Edit your cronjobs

crontab -e

Execute script every 30 min: (not the same as 30, which would do it every hh:30!)

*/30 * * * * /path/to/your/script

Restart cron. How depends on distro, here's Ubuntu:

service cron restart

List cronjobs:

crontab -l
Community
  • 1
  • 1
timss
  • 9,982
  • 4
  • 34
  • 56
  • Thanks, I'll combine your two answers, should really help. – user2298964 Apr 20 '13 at 15:13
  • @user2298964 I updated my answer, I was incorrect with the timing. It should be `*/30` to execute every 30 minute. Just `30` would be every time it's `hh:30` (half-something). – timss Apr 20 '13 at 15:20
0

I would personally run the script using following crontab:

0 * * * * $HOME/changewallpaper.sh

which you can install as a user with this command

crontab -e

Other solutions include running daemon script from file ~/.xprofile

For more information please refer to

man crontab
man 5 crontab

Also check out this project Variety.

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105