Enforcing work-life balance with bash scripting, or targeted ways to kill Linux programs

» Linux, Tech

In the days of the pandemic (let it be damned) we more often than ever work from home and, somewhat naturally, use one device both for work and out of it — for relax, hobbies, whatever.

If you are one of these people, you for sure noticed an interesting phenomenon — working hours are no more, and you find yourself answering e-mails from your work mailbox late at night, for a simple reason that you use the same e-mail client for your work, personal, and university accounts, so you do not close it. Ever. And it would linger at your mind if you did not respond.

Living 24/7 between the keyboard and the chair is something what many of us find acceptable.

Working 24/7 not so much and it is sometimes hard to notice that we are in fact doing that.

Yes, I am a work-a-holic, and I need some help to forget about all this.

Let me show you some Unix command-line tools which will butcher the shit out of these pesky work programs. Manual pages conveniently linked.

Solutions?

One solution would be to keep two accounts and switch between them, or even have two computers.

This really works only when you do nothing but work during work hours. I like being able to check my personal e-mail, order something or check my uni assignments during breaks or when the code is compiling.1

Compiling

I naturally drifted back to using one account.

Then what?

What I usually did is that when I was done I closed all the windows pertaining to my job. Sounds trivial, but as I mentioned above, my email client still shouted notifications at me. I used to forget to close my Slack client, and it reminded me of its existence sooner or later. I had multiple PyCharm windows open and the forgotten work repo would get my attention it should not get.

Let’s script it, or killing processes by their name

In the end, I made a bash script which starts all I need for work and another that makes it all fuck off.

1
2
3
4
5
6
#!/bin/bash
# nohup makes the program not quit when the script finishes.
# & makes the program run in the background.
nohup thunderbird &
nohup ripcord &
nohup pycharm-professional &

The latter is named dość (Polish for enough) and it ruthlessly murders all this shit with killall:

1
2
3
4
killall thunderbird
killall ripcord  # Slack client I use
killall pycharm.sh
systemctl stop docker

Wait, what about your other e-mail boxes? Or personal PyCharm projects? Or these Docker containers you play around with?

Thunderbird, or killing processes by their launch command

What we need here are Thunderbird profiles. I run two Thunderbird instances, one for the work e-mail and one for everything else. To create them, you need to run thunderbird --ProfileManager, create profiles and from now on it will ask you at each startup which one you want to run.

Conveniently you can select a profile with a run-time argument, so put thunderbird -P Work in your work script.

Thunderbird profile manager.

The next step is to make enough kill only the instance running the work profile. Behold:

1
2
3
4
5
6
7
# Slaughtering the work mailbox.
THUNDERBIRD_PID=$(pgrep -a thunderbird | grep Work | cut -f1 -d" ")
if [ $THUNDERBIRD_PID ] 
then
        echo "'Only one more mail'? Fuck off with this non-sense."
        kill $THUNDERBIRD_PID
fi

pgrep -a prints PIDs of all Thunderbird instances along with the command used to launch them. We filter (grep) the instances by the ones launched with Work in command line and then use cut to get the first field (PID) and discard the others (launch command).

If there is one, we print out, ekhm, celebratory message and kill it. Easy.

PyCharm, or closing windows by their title

For PyCharm, we would like to close our work projects but leave our pet projects open. For this we will use wmctrl command.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Murdering the work project window

PYCHARM_PROJECT_NAME="work-project"
PYCHARM_WINDOW_ID=$(wmctrl -l | grep $PYCHARM_PROJECT_NAME | cut -f1 -d" ")
if [ $PYCHARM_WINDOW_ID ]
then
        echo "Time to code something you do not have to."
        echo "Closing the work project."
        wmctrl -ic $PYCHARM_WINDOW_ID
fi

wmctrl -l prints all windows. grep gets the window with the project name in title. cut gets the window ID.

wmctrl -ic sends the close signal to the window. Since it works exactly as if you pressed Alt-F4, PyCharm will ask you to confirm.

Docker Compose, or stopping unneeded containers

The last step is to step the containers started by the work project. In my case, they are hopelessly heavy and make me forget that my laptop is rather new and fast.

So instead of running docker-compose stop, let’s check if any containers are running, print a snarky message and kill ‘em.

1
2
3
4
5
6
DOCKER_COMPOSE_FILE="~/work-project/docker-compose.yml"
if docker-compose -f $DOCKER_COMPOSE_FILE ps | grep Up
then
        echo Stopping voluptuous containers.
        docker-compose -f $DOCKER_COMPOSE_FILE stop
fi

Enough is enough

The next step is to say enough and run the script. Every day.

Are you too much of a work-a-holic and are staying late anyway? Make your computer slaughter that shite when you should not be working. Your mental health will thank you.

The standard Linux tool to run commands at given time is cron. To add a new task, run crontab -e.

In there you can define a task:

1
2
# Numbers are minute, hour, day, month, day of week
0 19 * * * /usr/local/bin/enough

Let’s have time for stupid things

Hopefully this post showed you at least one new command to play with. Even if it did not, and you find my method asinine, maybe the post convinced you to make sure that you do not stay working longer than you should.

Take care of yourselves! And let me know what methods you have to deal with the problem.

  • Final version of dość with some healthy Polish swearing and waiting for the command to exit is here.
  • Final version of work with checking if the program is already running and starting them in different i3 workspaces is here.
  1. Nowadays it may be more appropriate to say “My Docker containers are rebuilding.”