Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Anacron question


    Hi everyone,

    I need to run a command at startup (or thereabouts), but only once every X days. I could probably write a shell script to keep track of how many days it has been since the command last ran, then run that shell script at startup. But I get the feeling that this is the kind of thing anacron was made for.

    Catch is, the command will display a dialog box to the user in Gnome, so it should actually run at Gnome login rather than at system startup. Can I just put the command in /etc/anacrontab? Or do I need to preface the command with something to change from root to user and wait till user is logged into Gnome? Or do I need to run a separate instance of anacron for the user?

    This is on a single-user desktop system running Ubuntu 10.04.

    Many thanks for any suggestions,
    grabled

  2. #2
    Is the main thrust of the script to send the dialog box to the user, or is that just one piece of it? I ask b/c maybe what you want is to schedule your script (or some part of it) via cron or whatever, and have it write pertinent info to some file, say ~/.script-output.log. Then when your user logs in, you can read that file and act on it (by sending a dialog box or whatever).

    In any case, executing something when the user logs in can be done many ways. One way is to simply call your script from the bash login scripts (e.g., ~/.bashrc, ~/.bash_profile). You could do something like:

    Code:
    if [ -n "$DISPLAY" -a -x ~/myscript.sh ]; then
      ~/myscript.sh
    fi
    This will check first for the DISPLAY environmental variable to be set (which is done by your display manager), and for the presence of the executable script. Instead of using DISPLAY, you could check specifically for GNOME to be running with:
    Code:
    pidof gnome-session
    If your script is cronned and you are just reading a file at login, then you could do:
    Code:
    if [ -s ~/.script-output.log -a -n "$DISPLAY" ]; then
      # code to send dialog, etc.
    fi
    You could also use the ~/.xsession (if using gdm/xdm, and ~/.xinitrc if using startx) file to launch your scripts. These scripts are X-specific login scripts (think .bashrc for X). I'm not sure if they are still honored by X/Gnome, but they worked back in the day.

    You can also read up on gdm/xdm (Gnome/X display managers) - they support conventions for launching custom scripts. Typically it is a simple as putting a script in the proper dir (guessing: /etc/X11/xinit/xinitrc.d/) and making it executable.

  3. #3
    Thanks, atreyu.

    The main point is to run the script at login, but no more than once per week. (It is a nag, but nagging every time I startup/login is too much.)

    So whatever method I use to run the script, it needs to be able to check when I last ran the script. Initially I thought of doing something like you suggest--run a wrapper script at login, have the wrapper check when it last ran the main script, run the main script, then record the timestamp to a file somewhere. But isn't that what anacron already does?

  4. $spacer_open
    $spacer_close
  5. #4
    Yes, apparently that is what anacron is for (never used it but just read about it - very enlightening). but it does not solve your dialog-at-login problem, though. It appears you still need the anacron job to log its output somewhere and have a login script read that log. At least as far as I can tell...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •