Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an bash script that runs on a every minute cron. It checks to see if a necessary program is running and starts it if it is not. Occasionally it will run before the user has actually autologged in. I want to prevent that from happening.

What I think I'd like to do is to put a check in the check the computer's uptime and see if it'd been on for 5 minutes so that auto login had a chance to finish before the program gets started. I'm fairly new to bash and parsing what I get from "uptime" to where I could use it for comparison seems out of my reach.

Can anyone help? Perhaps there is a better solution. I'm stuck.

share|improve this question
up vote 13 down vote accepted

A bash script/cron job is the wrong solution for the problem you are trying to solve. Actually, scratch that -- two bash scripts/cron jobs are the wrong solution for your problem. ;)

The problem you are trying to solve is, "ensure a task starts running after a user has logged in, and ensure it stays running". This is a job for upstart.

The following should do what you want, and it goes into /etc/init/. Name the file something like user-session-job.conf.

description "Keep my very important program running"

start on desktop-session-start
stop on desktop-shutdown


end script

Please see the upstart cookbook for lots more help:

share|improve this answer
Thanks a ton. There are just so many "mostly right" ways to do things in Linux. It's awesome when you find the "right" one. – rabid.sloth Nov 9 '11 at 15:07

Since I had to alter the script drastically from the original, I decided to post this as a separate answer. I'm not sure why the code above does not work. I apologise, I should have tested it before posting it. I have verified that the code below however, does in fact work.


upSeconds="$(cat /proc/uptime | grep -o '^[0-9]\+')"
upMins=$((${upSeconds} / 60))

if [ "${upMins}" -gt "5" ]
    echo "Up for ${upMins} minutes"
    echo "Up five minutes or less"
share|improve this answer
Thanks! This is relatively similar to what I went with! I'll post my solution shortly! – rabid.sloth Nov 14 '11 at 15:34

Try this

#   upt - show just the system uptime, days, hours, and minutes

let upSeconds="$(cat /proc/uptime) && echo ${temp%%.*})"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
if [ "${days}" -ne "0" ]
   echo -n "${days}d"
echo -n "${hours}h${mins}m"
share|improve this answer
I get this error: [edit, sorry wrong link, fixed] – Matt Nov 9 '11 at 3:07
Mind including an explanation of the script? – N.N. Nov 9 '11 at 19:06
this script was copy & pasted from the link just above the code. There is an explanation of the code on that page. – druciferre Nov 10 '11 at 1:09

Your Answer


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.