I am having trouble figuring out how to set up my first cron job. I simply want to run this command once every week:

dpkg -l > ~/Dropbox/installed_packages

My /etc/crontab file contains the line

7 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )

So I need to place my command somewhere in the directory /etc/cron.weekly - but in which file? /etc/cron.weekly currently contains the following files:

apt-xapian-index  man-db  0anacron  cvs
  • Just out of curiosity, why? BTW, usually those kind of long piped instructions won't work on cron. You'll need a single script to be called.
    – Eldelshell
    Aug 20, 2010 at 8:28
  • See also /var/backups/dpkg.status* and /var/backups/aptitude.pkgstates*
    – derobert
    Aug 20, 2010 at 18:30
  • @Ubersoldat it's so I have a backup of the current state of my machine. I can import this list to a new machine or fresh installation. Aug 21, 2010 at 0:17
  • Actually the command should be dpkg --get-selections, but you get the idea... Aug 21, 2010 at 0:21
  • @Ubersoldat: Piped instructions should work just fine. Cron executes commands with /bin/sh, which takes care of all the pipes and so forth. May 9, 2013 at 21:14

3 Answers 3


Create a file with the following content (e.g. list_packages.sh):


dpkg -l > ~/Dropbox/installed_packages

Place this file in /etc/cron.weekly/ and it will run once a week.

  • Just curious: why /bin/bash instead of /bin/sh? I realize the original poster is running Linux, and will likely have /bin/bash, but your script doesn't depend on any bash-specific features.
    – drench
    Aug 21, 2010 at 15:27
  • @drench No real reason. I just typed /bin/bash out of habit. You're correct in that /bin/sh would have worked just fine. Aug 22, 2010 at 4:40

Save this as a script, say installed_packages_list.sh:

dpkg -l > ~/Dropbox/installed_packages

Make it executable.

chmod u+x installed_packages_list.sh

Then run

crontab -e

This will open up a file in a text editor. Type this and save

@weekly /full/path/to/script

The script will be executed once a week, with privileges of the user who scheduled the cron job.


First, for doing this you do not need to execute the job as super user.

The easiest way to solve this is to edit the crontab of your normal user via

crontab -e

Depending on you EDITOR enviroment variable, the users crontab is opened with your favourite text editor.

There you can add something like

# m h dom mon dow command
5 12 * * 1 bash $HOME/pkg.backup.sh

You have then to create the pkg.backup.sh file with your dpkg command in it.

Btw, if you want to be able to easily restore your current package state of the machine (aka package selection), then this command line is more target-oriented:

dpkg --get-selections > foo

To restore it, you just have to type

dpkg --set-seclections < foo

Actually, dpkg -l is useful in addition to that if you want to log exact package version number, too.

You must log in to answer this question.

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