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.

  • This would be extremely easy to script. Would starting the application do, to make it check? If so, what is the command to run it? – Jacob Vlijm Dec 24 '15 at 19:41
  • With cron you can clearly start programs. You could also cron a kill -15, but it depends on the application whether it handles it as a gentle nudge to shut down, ignores it or just crashes. – Harald Dec 24 '15 at 19:42

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.)

python3 <script> <command_to_run_application> <cycle_time> <application_run_time> force

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

#!/usr/bin/env python3
import subprocess
import time
import sys

force = False
args = sys.argv[1:]; app = args[0].replace("'", "")
proc = app.split()[0].split("/")[-1]
cycle = int(args[1])*60; run = int(args[2])*60

    if args[3] == "force":
        force = True
except IndexError:

def get_pid(proc_name):
        return subprocess.check_output(
            ["pgrep", proc_name]
    except subprocess.CalledProcessError:

def kill(pid, force):
    if force == False:
        subprocess.Popen(["kill", "-s", "TERM", pid])
    elif force == True:
        subprocess.Popen(["kill", pid])

while True:
    subprocess.Popen(["/bin/bash", "-c", app])
    pid = get_pid(proc)
    if pid != None:
        kill(pid, force)
    time.sleep(cycle - run)

To use

  • Copy the script into an empty file, save it as cycle_run.py
  • Run it with the command:

    python3 /path/to/cycle_run.py <command> <cycle_time> <application_run_time> force


    • <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.:

python3 /path/to/cycle_run.py 'gedit /home/jacob/Desktop/test.sh' 30 5

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.

  • Dear @JacobVlijm, you have an amazing skillset! Thank you very much for this Christmas present. Your script works great. Same as "Take a Break", I could imagine this script to turn into a nice, simply GUI where the user can define the application(s), the cycle time and the application run time. This GUI could then run at startup to prepare applications such as Telegram or Skype. What do you think? – orschiro Dec 25 '15 at 9:06
  • 1
    @orschiro sounds perfect! The gui is almost ready to use, with a few changes :). Have a nice Christmas! – Jacob Vlijm Dec 25 '15 at 9:09
  • would it be possible to force applications to open minimised or in background? – orschiro Dec 28 '15 at 7:54
  • @orschiro off course, what do you have in mind? – Jacob Vlijm Dec 28 '15 at 14:10
  • 1
    Hi @orschiro added a section to the answer. – Jacob Vlijm Dec 29 '15 at 9:18
  1. Edit your cron with crontab -e and add this line into your crontab to run it as schedule job for every 3 hours

    00 */3 * * * * ~/killtelegram.sh >/dev/null 2>&1 

2.Open your terminal and type below command

touch ~/killtelegram.sh
chmod +x ~/killtelegram.sh

open killtelegram.sh with your favorate editor and write as below

telegram &
sleep 15m
pkill telegram

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.

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.