Start and stop application from terminal in Linux Mint 19 / Ubuntu 18.04

Start and stop application from terminal in Linux Mint 19 / Ubuntu 18.04

In this article we will review several scenarios how to :

  • Start application from terminal - Ubuntu / Linux Mint
  • Schedule application start
  • Build script starting application
  • Stop application by id/name in Linux Mint / Ubuntu
  • Application stop with delay
  • Auto logout from Ubuntu / Linux Mint
  • Bonus tip: schedule application stop

Start application from terminal - Ubuntu / Linux Mint

In order to start application from terminal you need to know the name of the application which is used in the system. Sometimes the system name can differ from the application name. For example:

  • Chrome - google-chrome
  • Chromium - chromium-browser
  • Sublime - subl

You can find where a given application is installed by:

which google-chrome

result:
/usr/bin/google-chrome

Also there you can search for applications.

In order to start give application you need to write it's name in the terminal:

google-chrome

You can pass different parameters to the application which will be opened. For example which file to be opened for Libre office:

libreoffice --calc /home/user/now.csv

or what web sites to be opened with chrome:

google-chrome softhints.com google.com &

Schedule application start

Sometimes you may want to schedule a script for opening a given application. In order to improve my performance and decrease the distractions I'm scheduling my mail to be opened two times per day: 10:00 and 16:00.

The scheduling in Linux usually is done with cron jobs:

  • Open terminal
  • Type: crontab -e - for current user only or sudo crontab -e
  • finally add your job:

Some application can be started by their name but others will need some settings. For example in order to start thunderbird and display it you need to add this line in your crontab file:

0 16 * * * DISPLAY=:0 thunderbird %u

the same way if you are using bash script to start another application. For example starting LibreOffice calc:

0 10 * * * DISPLAY=:0 /home/user/LibreOffice.sh

where libre office will be started after a csv file with data is downloaded from a website.(I'll add new article on this topic).

Build script starting application

Building special scripts to start application can be helpful in order to automate boring stuff. I have 10 scripts which are helping me to do boring stuff like:

  • reporting
  • getting information from the web
  • sending mails
  • analysing data
  • starting and stoping application - obviously from the article :)

Below you can find several examples below:

#!/bin/bash
echo "Executed!" >> /home/user/myfile.txt
bash -c "/usr/bin/thunderbird"
  • the first row is going to "log" each time this scripted is executed
  • the second one is opening thunderbird

A more complex example where:

#!/bin/bash
cd /home/user/environments/venv36
source ./bin/activate
cd /home/user/spiders/

now=$(date +"_%Y-%m-%d")

scrapy runspider google.py -o - >data/google/google$now.csv -t csv
python helpers/pandas_after.py

libreoffice --calc /home/user/data/google/google$now.csv

this example is more complex. Here is what is done:

  • activate special python virtual environment
  • change to a special folder
  • run a web spider in order to get information from Google
  • analyze and change the data
  • load the download data in calc

this script is run with a cron job every day and retrieves important information for me. If I need to do the same thing I'll need to spend 10 to 30 minutes per day.

Stop application by id/name

To stop application from the terminal by id you will need to find the id first. This can be done by this command:

pidof google-chrome
pidof firefox

the output is:

10882 10869 10831 10784 10781

Another alternative is:

pgrep google-chrome

result:

10882 10869 10831 10784 10781

or the more verbose one:

ps axu | grep google-chrome

user 2315 0.0 0.0 15648 1152 pts/1 S+ 18:38 0:00 grep --color=auto google-chrome
user 10781 0.3 0.4 1379564 151624 ? SLl 14:57 0:47 /usr/lib/..

and then you can extract the pid-s.

After that in order to stop the application you can use:

kill 10781
kill -9 10781

both do similar things but the one with -9 is more "aggressive" and forcing.

In order to stop/kill application by name you can use:

pkill firefox

this is going to kill multiple processes by name.

Application stop with delay

Now let say that you want to stop a given application with some delay: 10 minutes, 1 hour or something else. You can do with by several different ways: cron job, script, third party software. In this case we will use simple script:

#!/bin/bash
sleep 1h
pkill firefox

this script will wait for 1 hour and then will stop the application. So you can use a script to start a given application. This application to be executed for 1 hour and finally to be stopped.

Other times examples:

sleep .5 # Waits 0.5 second.
sleep 5  # Waits 5 seconds.
sleep 5s # Waits 5 seconds.
sleep 5m # Waits 5 minutes.
sleep 5h # Waits 5 hours.
sleep 5d # Waits 5 days.

Note: You can't use:

sleep 1m | pkill firefox

Auto logout from Ubuntu / Linux Mint

The final problem which we are going to cover in the article is about log out of inactive users. There is application for Ubuntu / Linux Mint which can be used - autolog. You can search in the software center and install it. Or you can install it by:

sudo apt-get update
sudo apt-get install autolog

Bonus tip: schedule application stop

Let say that you work a lot google chrome, slack, zoom, pycharm or any other application in Linux Mint, Ubuntu. Most of the time is really difficult to end up your working day and leave your workplace. Automation to the rescue! Below you can find a simple script and how to use in order to schedule stop for your applications in Linux:

#!/bin/bash
date '+%A %W %Y %X'
sleep 30m
pkill firefox

The script will show the current date and time. Then will wait for a given period of time ( in this case 30 minutes ) and finally will kill the application.

now you can schedule this script as a cronjob by:

  • type crontab -e in terminal
  • then add this line:
    • 0 16 * * * /home/user/cron/kill_firefox.sh
    • which is going to run the script at 16:00
    • the name of the script should be kill_firefox.sh and the path is: /home/user/cron/
Share Tweet Send
0 Comments
Loading...