Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What is the most convenient way of setting a crontab command to run every 59th second, other than running a sleep 59 in the beginning of my script?

If there are no other suggestions, will executing sleep 59 delay all my other cron entries scheduled for execution wait until the sleeping one completes?

Update Why do I need it at all? I've written a small script to find out who has logged on the system during the last minute, it looks like this:

who | grep `date +%R` | awk '{ print $1 " joins us." }' | write $USER

In order for it to work and not to get matters complicated, I need it to run in the end of the minute.

share|improve this question
    
See my edited answer. –  Dennis Williamson Oct 1 '09 at 21:03

6 Answers 6

up vote 8 down vote accepted

This has a date command that looks at the previous minute:

who | grep `date -d "now -1 min" +%R` | awk '{ print $1 " joins us." }' | write $USER

Then you could run it on the minute with cron:

* * * * * /some/dir/scriptname

You could even do this:

who | grep `date -d "now -60 sec" +%R` | awk '{ print $1 " joins us." }' | write $USER

Edit: Here's a freebie:

awk 'FNR > 2 {printf ",\n"} {printf "%s", name; name = $0; next} END {conj = " and\n"; if (FNR == 1) {singular = "s"; conj = ""}; printf "%s%s join%s us\n", conj, name, singular}'

Output with commas, "and" and plural verb agreement and removal of repeated phrase (you can replace the newlines (except for the last one) with spaces if you want it all on one line):

allen joins us

or

bill and
chris join us

or

dave,
ellen and
felicity join us

share|improve this answer
    
Thanks, that should do the job! –  dpq Oct 1 '09 at 17:04

The best you can do using cron is run it every minute:

# minute hour dom mon dow
* * * * * /path/to/command
share|improve this answer
    
That doesn't answer my question, sorry. –  dpq Oct 1 '09 at 16:16
8  
Yes it does, your question was how to run a crontab every 59th second. MikeyB correctly answered that you can't. –  Scott Lundberg Oct 1 '09 at 16:20
    
I stand corrected. –  dpq Oct 1 '09 at 17:10

sleep 59 wouldn't delay other tasks.

But it smells like danger. If you need that level of granularity then I predict you will come unstuck.

If you explain the reason for your requirements you might find that someone is able to offer a better cure.

share|improve this answer

cron is just not that accurate. If you need that level of precision you should consider writing a daemon.

Have you considered using tail -f on the auth.log and just watching for logins constantly?

share|improve this answer

You'd be better off adding the script to all users .login file, or .bashrc, or similar.

1) It'd only run when someone logged in, so more efficient. 2) it would run instantly when someone logged in, so no delay.

share|improve this answer
    
I'm not root on that particular machine I want to install this thing on. –  dpq Oct 1 '09 at 16:54

I'll just mention that zsh has a watch builtin that does just this. (Though, you must be using zsh, and using your shell, to see it: it won't pop up in other terminals.)

In brief, add this to your .zshrc:

watch=notme
share|improve this answer

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.