Linux - run script every 5 mins as long as user is logged in

0

I need to run a script when a user logs in and need to run the script every 5 minutes as long as the user is logged in. Script should run within the users security context and should stop running when the user logs out. I manually added crontab lines for different users:

*/5 * * * * whoami >> /tmp/<username>.txt

The script really started running in each users context, but it never stopped running. Even after users logged out, script continued adding lines to the text files in tmp folder.

Is there any way to run a script every 5 minutes in logged in users context until logging out?

Abdullah Nehir

Posted 2019-10-23T08:58:04.623

Reputation: 103

2One way is to check if there is active session and exit if id isn't. How to check depends on which types of sessions you want to cover (GUI login, console tty login, ssh, ftp etc.). Also, you might only need to have a single script in the root crontab to find all users that currently logged in, then run something on their behalf, and to find out which users are active may be as simple as checking wtmp with the 'last' command. – Nikita Kipriyanov – 2019-10-23T09:13:37.887

1This might help: replace whoami with users | grep -q "\b$USER\b" && whoami – Cyrus – 2019-10-23T11:30:21.357

Answers

1

I'd suggest a systemd solution: You'll need a service file, e.g. my5min_script.service

[Unit]
#just what it does
Description= run script every 5 min as long as user is logged in

[Service]
#we assume the full service as active one the script was started
Type=simple
#where to find the executable
ExecStart=/path/to/script
#what you want: make sure it always is running every 5 minutes
Restart=always
RestartSec=5min

[Install]
#which service wants this to run - default.target is just it is loaded by default
WantedBy=default.target

Now place this file in /etc/systemd/user/ and activate is via

systemctl --user --global enable my5min_script

Like that the script is a) run as user b) by every user (global) and the start and end is bound to a user-specific default.target, that is activated and stopped with the login/logout.

A few more details are to be found on Archwiki.

Fiximan

Posted 2019-10-23T08:58:04.623

Reputation: 199

Hi Fiximan, thank you for the solution, I tried it but the service did not start when user logged in. I had to start it from the command line. Also it did not stop when the user logged out, it continued to run every 5 mins. I can solve auto start problem by starting the service in /etc/profile. How can I make it stop when the user logs out? – Abdullah Nehir – 2019-10-23T14:51:23.177

Can you check systemctrl --user status <servicename> after login? – Fiximan – 2019-10-23T14:55:33.017

I checked it and it was not started – Abdullah Nehir – 2019-10-23T15:08:36.050

1By the way, its systemctl right? Not systemctrl... – Abdullah Nehir – 2019-10-23T15:09:13.377

Yes, typo from my side. Did the user have any other session open? It should start with the first session of the user and end with the last one closed. If the service is enabled while a session is open, it'll not be triggered. – Fiximan – 2019-10-23T15:10:25.187

Thank you, this morning I've restarted the computer and my5min_script service automatically started when a user logs in. But service did not stop after log out. I've just found a keyword named ExecStop in the service file. I will try using it with $MainPid – Abdullah Nehir – 2019-10-24T08:19:58.530

1I figured out why it did not stop: some processes started with the users context were still running. So literally users session was still alive. Whe I killed the processes my5min_script service stopped really. – Abdullah Nehir – 2019-10-24T09:26:13.260

Nice. If you want to check for an actual login, you could e.g. use commands like w, who, finger, users and add an abort mechanism in your script. – Fiximan – 2019-10-25T08:07:17.307