What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands tagged cronjob from sorted by
Terminal - Commands tagged cronjob - 13 results
crontest () { date +'%M %k %d %m *' |awk 'BEGIN {ORS="\t"} {print $1+2,$2,$3,$4,$5,$6}'; echo $1;}
2015-03-12 19:56:56
User: CoolHand
Functions: awk date echo

usage = crontest "/path/to/bin"

This version of this function will echo back the entire command so it can be copied/pasted to crontab. Should be able to be automagically appended to crontab with a bit more work. Tested on bash and zsh on linux,freebsd,aix

for USER in /var/spool/cron/*; do echo "--- crontab for $USER ---"; cat "$USER"; done
2014-12-11 19:48:46
User: tyzbit
Functions: cat echo

This is not exhaustive but after checking /etc/cron* is a good way to see if there are any other jobs any users may have set.

Note: this is a repost from a comment "flatcap" made on http://www.commandlinefu.com/commands/view/3726/print-crontab-entries-for-all-the-users-that-actually-have-a-crontab#comment, for which I am grateful and I take no credit.

echo $[RANDOM % 100] # range 0-99
2013-05-25 19:02:00
User: anapsix
Functions: echo

use it to stagger cronjob or to get a random number

increase the range by replacing 100 with your own max value

sleep $((3600 - ($(date +%s) % 3600) ))
2012-12-09 16:21:57
User: Mozai
Functions: date sleep

pauses exactly long enough to wake at the top of the hour

( di $TOFSCK -h ; /bin/umount $TOFSCK ; time /sbin/e2fsck -y -f -v $FSCKDEV ; /bin/mount $TOFSCK ) |& /bin/mail $MAILTO -s "$MAILSUB"
2010-10-24 00:35:23
User: px
Functions: time

This one-liner is for cron jobs that need to provide some basic information about a filesystem and the time it takes to complete the operation. You can swap out the di command for df or du if that's your thing. The |& redirections the stderr and stdout to the mail command.

How to configure the variables.




FSCKDEV=`grep $TOFSCK /proc/mounts | cut -f1 -d" "`

MAILSUB="weekly file system check $TOFSCK "

*/5 * * * * root /usr/local/nagios/sbin/nsca_check_disk 2>&1 |/usr/bin/logger -t nsca_check_disk
2010-07-02 00:47:05

This command will log the output of your simple cronjobs to syslog, and syslog will take it from there. Works great for monitoring scripts which only produce simple output.


* This can be used by regular users, without modifying system files like /etc/syslog.conf

* Reduce cron spam to root@localhost (Please stop spaming the sysadmins)

* Uses common tools like syslog (and logrotate) so that you don't need to maintain yet another krufty logfile.

* Still ensures that the output is logged somewhere, for posterity. Perhaps it's stored the secure, central syslog server, for example.

* Seems to work fine on Ubuntu, CentOS, FreeBSD & MacOSX

H=$(until ([ $i -le 6 -a $i -gt 0 -o $i -le 23 -a $i -gt 21 ] ); do i=$(date +%N | cut -c8-10); done ; echo $i) ; M=$(until [ $i -le 59 ]; do i=$(date +%N | cut -c8-10); done ; echo $i) ; echo $M $H \* \* \* backup-rsync-push.sh
0 10 * * * rsync -rau /[VIPdirectory] X.X.X.X:/backup/[VIPdirectory]
2010-03-02 17:48:54
User: mack
Functions: rsync

With this cron, rsync begins to sinchronize the contents of the local directory on /[VIPdirectory] with the directory /backup/[VIPdirectory] on the remote server X.X.X.X. Previously we need working on public/private-keys ssh to guarantee the acces to the remote server on X.X.X.X

mail -s "subject" user@todomain.com <emailbody.txt -- -f customfrom@fromdomain.com -F 'From Display Name'
2010-01-18 19:55:27
User: dmmst19
Functions: mail
Tags: cronjob mail

It's very common to have cron jobs that send emails as their output, but the From: address is whatever account the cron job is running under, which is often not the address you want replies to go to. Here's a way to change the From: address right on the command line.

What's happening here is that the "--" separates the options to the mail client from options for the sendmail backend. So the -f and -F get passed through to sendmail and interpreted there. This works on even on a system where postfix is the active mailer - looks like postfix supports the same options.

I think it's possible to customize the From: address using mutt as a command line mailer also, but most servers don't have mutt preinstalled.

echo "nohup command rm -rf /phpsessions 1>&2 &>/dev/null 1>&2 &>/dev/null&" | at now + 3 hours 1>&2 &>/dev/null
2009-08-18 07:31:17
User: AskApache
Functions: at echo

This is helpful for shell scripts, I use it in my custom php install script to schedule to delete the build files in 3 hours, as the php install script is completely automated and is made to run slow.

Does require at, which some environments without crontab still do have.

You can add as many commands to the at you want. Here's how I delete them in case the script gets killed. (trapped)

atq |awk '{print $1}'|xargs -iJ atrm J &>/dev/null

chronic () { t=$1; shift; while true; do $@; sleep $t; done & }
2009-06-13 05:57:54
User: rhythmx
Functions: sleep

Chronic Bash function:

chronic 3600 time # Print the time in your shell every hour chronic 60 updatedb > /dev/null # update slocate every minute

Note: use 'jobs' to list background tasks and fg/bg to take control of them.

[[ $(COLUMNS=200 ps faux | awk '/grep/ {next} /ssh -N -R 4444/ {i++} END {print i}') ]] || nohup ssh -N -R 4444:localhost:22 user@relay &
2009-03-31 09:39:59
User: j0rn
Functions: awk nohup ps ssh
Tags: ssh cronjob

I find it ugly & sexy at the same time isn't it ?

lynx -accept_all_cookies -cmd_script=/your/keystroke-file
2009-03-17 00:38:36
User: Alanceil

This command will tell lynx to read keystrokes from the specified file - which can be used in a cronjob to auto-login on websites that give you points for logging in once a day *cough cough* (which is why I used -accept_all_cookies).

For creating your keystroke file, use:

lynx -cmd_log yourfile