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?
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
withusers | grep -q "\b$USER\b" && whoami
– Cyrus – 2019-10-23T11:30:21.357