Exemplary use case:
I would like to start Telegram Desktop periodically every 3 hours for 15 minutes, to check for newly incoming messages. After 15 minutes, the application should close again and re-launch after the next 3 hours.
Exemplary use case:
I would like to start Telegram Desktop periodically every 3 hours for 15 minutes, to check for newly incoming messages. After 15 minutes, the application should close again and re-launch after the next 3 hours.
Cron versus background script
Of course the very first thing that pops into the mind is to use
cron
. Whether you use cron, or a small background script is mainly a matter of taste.The advantage of cron is that it hooks up on an existing process (although a script adds, well, actually nothing to the processor load).
The advantage of a background script is that is more flexible; simply kill it and run it with other arguments if you'd like to change time- or other settings. You can also re- use it with other applications without having to do another setup, just a command is enough.
The script below can be run by the command (e.g.)
Where the last argument, if set, forcefully kills the application. If not set, the application will close gracefully, to make sure possible changes etc. will not be lost.
The script
To use
cycle_run.py
Run it with the command:
where:
<command>
is the command to run the application (without the--%u
-section, in my case, copied from the.desktop
file:/home/jacob/Downloads/Telegram/Telegram
)<cycle_time>
is the (total) cycle time in minutes (3 hours = 180 in your example)<application_run_time>
is the time the application should run in minutes (15 in your example)force
is an optional argument, to forcefully kill the application. Simply leave it away to gracefully kill the application.Running applications with arguments
If you run an application with arguments, make sure you use quotes around the command to run the application, e.g.:
Running applications minimized or in tray
Starting and terminating applications periodically will often be needed only minimized and/or in tray. As requested by OP, a few remarks on that:
If an application offers starting up in tray from command line, simply use the argument to do so. In the case of
Telgram
, the argument to use is:although the option seems not to work on all systems (it does on mine), as mentioned here. You will have to test in your situation.
If the application does not offer the command line option to startup minimized or in tray, I'd suggest using the (this) script in combination with the one here (I'd suggest the
pid
- version), which will make it possible to startup the application minimized.Edit your cron with
crontab -e
and add this line into your crontab to run it as schedule job for every 3 hours2.Open your terminal and type below command
open killtelegram.sh with your favorate editor and write as below
Save and exit
thats it. It will open automatically for every 3 Hours and it will stay for 15 min and It will get killed.