6

I have a bash script that should be executed on ubuntu startup after boot.

The problem is the script should be only executed once a day. So when the script recognizes that it already has been executed today it should stop itself. How can i do this?

2 Answers 2

15

As Ubuntu uses Anacron by default, you already have this behaviour by putting the script into /etc/cron.daily. Check run-parts --test /etc/cron.daily to check if your script is picked up (importantly, no .sh extension and the +x bit set). See this wiki page and man anacron for more detailed information about why this will work for you.

3
  • thanks. i'm not so familiar with cron. is it possible to run the script directly after boot process finished?
    – NES
    Dec 4, 2010 at 13:53
  • 1
    As I understand it, anacron is run at startup (by /etc/init/anacron.conf), which will cause the script to be run on startup.
    – Iain Lane
    Dec 4, 2010 at 14:02
  • If the behavior you are looking for is for your script to execute each time you boot your computer, then you would want to execute your script from rc.local as Marlon discussed. If what you want is for your script to execute once (and only once) per day, regardless of how many times you boot your computer during the day, then you would definitely want to use anacron as Lain Lane stated. Dec 5, 2010 at 17:12
1

If you have a desktop it's easy to do using the GUI: howtogeek article.

If you have a headless server then Iain Lane's answer should be good enough but you could also edit /etc/rc.local just make sure your script ends with exit 0. rc.local is the "preferred" place for startup scripts but cron daily works just fine. Use crontab -e to edit your own crontab file. Here is a nice little Howto on using cron.

2
  • Please note that the howtogeek article linked above refers to an older version of Ubuntu (Edgy Eft) and that the menus have been rearranged since then. The current menu path to the Startup Applications GUI is System->Administration->Startup Applications in Ubuntu 10.04 Karmic.
    – koanhead
    Dec 4, 2010 at 23:06
  • You are correct and I did see that it was for an older version but at the time I wanted a quick sample showing there was a GUI way. I will edit the link if I find another one more current. Then again I have no idea which version he is using.
    – Marlon
    Dec 6, 2010 at 4:54

You must log in to answer this question.

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