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 using crontab for the first time. Want to write a few very simple test cron tasks, and run them.

$crontab * * * * * echo "Hi"

doesn't produce anything.

crontab */1 * * * * echo "hi"

says */1: No such file or directory.

Also, how do I list the currently running cron tasks (not just the ones I own, but ones started by other users such as root as well).

And how do I delete a particular cron task?

share|improve this question

3 Answers 3

up vote 8 down vote accepted

You can't use crontab like that. Use man crontab to read about the correct way of calling this utility.

You'll want to use crontab -e to edit the current user's cron entries (you can add/modify/remove lines). Use crontab -l to see the current list of configured tasks.

As for seeing other user's crontabs, that's not possible without being root on default installations. See How do I list all cron jobs for all users for some ways to list everything (as root).

Note: be very careful when you use shell globbing characters on the command line (* and ? especially). * will be expanded to the list of files in the current directory, which can have unexpected effects. If you want to pass * as an argument to something, quote it ('*').

share|improve this answer
    
so I do crontab -e and in editor I put entry */1 * * * * echo "hi" and when I save this file, I see the message that crontab: installing new crontab. but I don't see the hi messages appearing on screen. What am I missing now? thanks. –  xyz Sep 23 '11 at 10:20
2  
crontab's output will never go to your "screen". From the POSIX man page for crontab: "If standard output and standard error are not redirected by commands executed from the crontab entry, any generated output or errors will be mailed, via an implementation-dependent method, to the user." –  Mat Sep 23 '11 at 10:23
1  
The job's output will be sent to the job owner's local mail box. (Running mail from the command line should access your local mail box, if configured.) To see the 'hi' in graphic environment, you could set the task as: * * * * * export DISPLAY=:0; xmessage 'hi'. –  manatwork Sep 23 '11 at 10:30
    
@manatwork You may also need XAUTHORITY, which may not be easy. –  derobert Sep 20 '12 at 21:53

If you want to modify the crontab interactively, run the command crontab -e, with no other option. This will start an editor on a copy of the crontab; when you exit the editor, the edited copy will become the new crontab. You can control which editor is started through the VISUAL and EDITOR environment variables. To list your crontab, run crontab -l.

If you want to modify the crontab in a script, set VISUAL and EDITOR to the path to a script or to a shell snippet that modifies the supplied file in place. The editor ed is a possibility here, or sed -i if your implementation of sed has this option. If you want to unconditionally add a line, you can use echo … >>. Take care with quoting; if at all in doubt, write a script and pass the name of the script as EDITOR.

script=$(mktemp)
cat <<'EOF' >"$script"
#!/bin/sh
ed -s "$1" <<'EOS'
g/^ *[^= ][^ =]*  *[^= ][^ =]*  *[^= ][^ =]*  *[^= ][^ =]*  *[^= ][^ =]*  *echo "hi"$/d
$a
* * * * * echo "hi"
.
w
q
EOS
EOF
share|improve this answer

For instruction on how to edit and create cron jobs and an explanation on the files structure see this crontab syntax guide

share|improve this answer
1  
Please add at least a short summary of the article you link to. As it is, when that link goes dead, your post will have absolutely no value. –  Mat Sep 22 '12 at 14:30

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.