Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
    
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 '10 at 8:28
    
See also /var/backups/dpkg.status* and /var/backups/aptitude.pkgstates* –  derobert Aug 20 '10 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. –  Internet man Aug 21 '10 at 0:17
    
Actually the command should be dpkg --get-selections, but you get the idea... –  Internet man Aug 21 '10 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. –  Keith Thompson May 9 '13 at 21:14
add comment

3 Answers

up vote 6 down vote accepted

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

#!/bin/bash

dpkg -l > ~/Dropbox/installed_packages

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

share|improve this answer
    
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 '10 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. –  David Narayan Aug 22 '10 at 4:40
add comment

Save this as a script, say installed_packages_list.sh:

#!/bin/bash
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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.