1

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?

2
  • 3
    One 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. Oct 23, 2019 at 9:13
  • 1
    This might help: replace whoami with users | grep -q "\b$USER\b" && whoami
    – Cyrus
    Oct 23, 2019 at 11:30

1 Answer 1

3

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.

8
  • 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? Oct 23, 2019 at 14:51
  • Can you check systemctrl --user status <servicename> after login?
    – FelixJN
    Oct 23, 2019 at 14:55
  • I checked it and it was not started Oct 23, 2019 at 15:08
  • 1
    By the way, its systemctl right? Not systemctrl... Oct 23, 2019 at 15:09
  • 1
    I 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. Oct 24, 2019 at 9:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .