Saturday, September 18, 2010

Some crontab usages

1. Run a command at boot time
In /etc/crontab add:

@reboot root command

Replace root with username to run as regular user:


@reboot phan vnc4server :1 -geometry 1200x800 &


2. Run GUI programs
To run a GUI program by cron, we have to tell what display should be used by:

DISPLAY=:0 or export DISPLAY=:0

For example, to change fluxbox background automatically twice per hour, at 0 and 30 minute, run crontab -e, then add the following line:

0,30 * * * * DISPLAY=:0 fbsetbg -f -r $HOME/.fluxbox/backgrounds/

Change display number if needed, :0 will be right for most cases.

3. Daily backup to Dropbox at midnight
Run crontab -e, then add:

# run five minutes before midnight, every day
55 23 * * * $HOME/Dropbox/bin/b2d.daily && $HOME/out 2&>1

b2d.daily is a shell script:

$ cat $HOME/Dropbox/bin/b2d.daily


#!/bin/bash
tar czvf $HOME/Dropbox/My-Dir-`date +%F`.tar.gz \
$HOME/My-Dir && DISPLAY=:0 dropbox start

Here we see another way to run GUI programs by cron. Of course, you can use 2 continuous cron jobs, first — run tar, second — dropbox, instead of using b2d.daily script.

P.S. share your cron usages, if there're no top secrets.

No comments: