我想要以下行為:如果我已經登錄,並且有一定時間,並且程序尚未啟動,請啟動該程序。我該如何實現?
如果我已登錄,如何在預定的時間啟動程序?
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
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.
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.
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