7

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.

14

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

respawn

script
    /path/to/my/very/important/program
end script

Please see the upstart cookbook for lots more help: http://upstart.ubuntu.com/cookbook/

  • 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
3

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.

#!/bin/bash

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

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

Try this

#!/bin/bash
#
#   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" ]
then
   echo -n "${days}d"
fi
echo -n "${hours}h${mins}m"
  • 1
    I get this error: [edit, sorry wrong link, fixed] pastebin.com/qVUXpeBC – 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. – Drew Chapin Nov 10 '11 at 1:09
1

This would be enough:

test $(cut -d '.' -f1 /proc/uptime) -gt 300

Script example:

if test $(cut -d '.' -f1 /proc/uptime) -gt 300; then
  echo "booted more than 5 minutes ago"
else
  echo "too early"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.