Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want the following behaviour: if I am logged in, and it is a certain time, and the program isn't already started, start the program. How can I achieve this?

share|improve this question
    
Please clarify, the program should start only when you login, e.g. let's say you need the app to start at 16:30, you logged in at 16:25, should program start 5 minutes later after you login or it will never start till you hit into this time frame, maybe on your next login? –  wik Jul 5 '12 at 17:09
add comment

4 Answers

Look at this command called at http://manpages.ubuntu.com/manpages/gutsy/man1/at.1posix.html you can execute commands at a specific time Use this to launch your application at a specific time

share|improve this answer
add comment

Assuming this application is a graphical application that requires an X display to connect to, I'd write a script that sleeps for X seconds then runs the application, where X is the number of seconds until the desired time. Then run this via Startup Applications.

Something like this:

#!/usr/bin/env bash

# run at 16:30
h=16 m=30

while true; do 
    # grab current hour and minute
    read now_h now_m < <(date "+%H %M")

    # calculate approximate number of seconds until 16:30 using modular 
    # arithmetics
    (( s = (1440 + (10#$h*60 + 10#$m) - (10#$now_h*60 + 10#$now_m)) % 1440 * 60 ))

    printf "Sleeping %d seconds\n" "$s"
    sleep "$s" && theapp
done

That should run the app somewhere between 16:30 and 16:31 each day while you're logged in.

share|improve this answer
add comment

I would write a script to be run by CRON.

To check if a user is logged in:

who -a | grep fred

Where fred is the user name you want to check for.

ps -ef | grep appname

Will return something if it finds appname running so you can check for that.

Make sure that you run the script with the appropriate user credentials (root?)

So, for example here is a bash script snippet that shows testing for the output from the cmd | grep ....:

#!/bin/bash

if who -a | grep fred > /dev/null; then
   echo "Fred is logged in"
   if ps -ef | grep appname > /dev/null; then
      echo "User is logged in and app is running"
   else
      echo "User is logged in BUT appname is not running"
      # ---- Do stuff here ----
   fi
else
   echo "Fred is NOT logged in"
fi

One thing to note is that the way I've coded grep means that it does a wild-card search by default so grep jul would find julian and july, etc.

share|improve this answer
add comment

Another option is to use Scheduled tasks with it can you can specify a lot of things https://apps.ubuntu.com/cat/applications/gnome-schedule/ download it at apt://gnome-schedule

share|improve this answer
add comment

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.