Schedule a job under Linux using cron
My last post was for a backup script that tar’ed up an entire Linux box (with the exception of some useless directories) and then uploaded it to an FTP server. Useful stuff, but a pain if you have to run it yourself manually every day! Enter, cron.
Cron is the daemon that executes commands according to a set schedule, and crontab is the utility used to manipulate it. To add a job, login as root and issue the command crontab -e. You’ll be presented with an editor for you to add the new job. The format the new line will need to format is as follows:
* * * * * command - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)
So, if we wanted our backup script (located at /back/dobackup.sh) to run at 10pm every night, you’d insert:
0 22 * * * /back/dobackup.sh
Save and exit (as you’re in vi, that’s the key sequence: escape : w q enter), and it’ll be added to your crontab. To check, issue: crontab -l
Did you find this hint useful? Or are you wondering how to learn more? Well, here’s a few books that I’ve found useful over the years:
![]() Linux Command Line and Shell Scripting Bible |
![]() Linux: The Complete Reference, Sixth Edition |
![]() Linux Pocket Guide – Essential Commands |
![]() Linux All-in-one Desk Reference |
Backup a Linux box through FTP (push) and tar
Another quick Linux how-to. This time how to backup up an entire box using into a tar archive and push it to an FTP server. In my case, this ran every night and the FTP server was the central tape backup unit.
It’s a good idea to create yourself a separate directory to store the backup shell script and FTP script. For want of a better name, I created /back (10 out of 10 for originality, right?). In there, first create the backup script dobackup.sh:
rm /back/*.tgz
d=$(date +%y%m%d)
tar cvpzf /back/back$d.tgz --exclude=/proc --exclude=/lost+found --exclude
=/back --exclude=/mnt --exclude=/sys /
lftp -f /back/backftpscript
[root@nas01 /]# chmod a+x dobackup.sh[root@nas01 /]#
open -u <ftp username>,<ftp password> <ftp host>mput /back/*.tgzbye
Are you wondering how to learn more about Linux? Well, here’s a few books that I’ve found useful over the years that might just help you more:
![]() Linux Command Line and Shell Scripting Bible |
![]() Linux: The Complete Reference, Sixth Edition |
![]() Linux Pocket Guide – Essential Commands |
![]() Linux All-in-one Desk Reference |
Creating and mounting an ext3 partition
[root@nas01 /]# fdisk /dev/hdbCommand (m for help): dNo partition is defined yet!(Note: If you’ve got an existing partition on the drive, d will remove the partition and you’ll get a message like “Selected partition 1″. If you’ve got any additional partitions, d them too)Command (m for help): nCommand actione extendedp primary partition (1-4)pPartition number (1-4): 1First cylinder (1-993, default 1): <hit enter>Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-993, default 993): <hit enter>Using default value 993Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.[root@nas01 /]# mkfs.ext3 /dev/hdb1mke2fs 1.41.8 (11-July-2009)Filesystem label=OS type: LinuxBlock size=1024 (log=0)Fragment size=1024 (log=0)125488 inodes, 500440 blocks25022 blocks (5.00%) reserved for the super userFirst data block=1Maximum filesystem blocks=6763315262 block groups8192 blocks per group, 8192 fragments per group2024 inodes per groupSuperblock backups stored on blocks:8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409Writing inode tables: doneCreating journal (8192 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 37 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@nas01 /]# mkdir /back
[root@nas01 /]# mount /dev/hdb1 /back
[root@nas01 /]# df -mFilesystem 1M-blocks Used Available Use% Mounted on/dev/hda1 1930 741 1090 41% /tmpfs 1980 0 1980 0% /dev/shm/dev/hdb1 474 11 439 3% /back[root@nas01 /]#
/dev/hdb1 /back ext3 defaults 1 1
Did you find this hint useful? Or are you wondering how to learn more? Well, here’s a few books that I’ve found useful over the years:
![]() Linux Command Line and Shell Scripting Bible |
![]() Linux: The Complete Reference, Sixth Edition |
![]() Linux Pocket Guide – Essential Commands |
![]() Linux All-in-one Desk Reference |





