How to run a job on first login of each day using cron or something similar?

5,784

Solution 1

use anacron:

Anacron can be used to execute commands periodically, with a frequency specified in days. Unlike cron(8), it does not assume that the machine is running continuously. Hence, it can be used on machines that aren't running 24 hours a day, to control daily, weekly, and monthly jobs that are usually controlled by cron.

read the whole manpage.

Solution 2

I wouldn't do that with cron - instead, configure the script to run whenever you log in, maybe by invoking it in ~/.bashrc or something like that. Then put some code at the beginning of the script to check the modification time on a marker file (you can choose any name and path you like for the marker file), and if it's been modified in the current day, make the script exit right away. After that, touch the marker file to update its modification time, and then the script can go on and do its business.

Share:
5,784

Related videos on Youtube

Akilan
Author by

Akilan

Updated on September 17, 2022

Comments

  • Akilan
    Akilan over 1 year

    I need to run a script when i login for first time in a day. I may reboot or shutdown and then power on during the day. But i do not want the job to run during those power ups. It should run only once per day.

  • David Z
    David Z almost 14 years
    Typo in your first line: anacron, not anachron.
  • Akilan
    Akilan almost 14 years
    I have a doubt. Can interactive programs be run using anacron?
  • akira
    akira almost 14 years
    define "interactive".
  • Akilan
    Akilan almost 14 years
    like for example gedit text editor.
  • akira
    akira almost 14 years
    so, "interactive" in the sense that it has a graphical user interface and is not waiting for some user input before it returns... that is still possible. you have to ensure that anacron (or the job anacron is starting) is launched after your X session is fired up. then you have to launch gedit with the DISPLAY environment set correctly and running under the correct user to launch gedit in the X session of the right user. but why would you want do this? search for the autostart mechanism of your desktop environment (kde, gnome, xyz), would be a better idea imho.
  • akira
    akira almost 14 years
    thats essentially what @David Zaslavsky said.
  • Akilan
    Akilan almost 14 years
    I use gnome and the gnome startup applications dialog doesn't have an option of run once per day/month etc.
  • Akilan
    Akilan almost 14 years
    David Zaslavsky also said the same thing. I can write such a script, but what i was looking for is an application which does this in a generic way. Anacron was very nice but only problem is that in my case i wanted it to run a gui app which expected some input from me. The reason to automate such a task is that i forget to do it daily. :p I'm willing to add such a functionality into gnome. Anyone know the package which executes autostart programs? (gnome-session-properties only adds or removes entries.)