Is there software to periodically start and close an application?

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

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

def get_pid(proc_name):
    try:
        return subprocess.check_output(
            ["pgrep", proc_name]
            ).decode("utf-8").strip()
    except subprocess.CalledProcessError:
        pass

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])
    time.sleep(run)
    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
    

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

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:

    -startintray
    

    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.


  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

#!/bin/bash
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.