You are not logged in.

#1 2015-09-20 15:56:31

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

systemd to launch script upon specific users?

Hello all,

A quick question that I cannot find an answer.

Can systemd be use to start a script upon a user login which will obviously be terminated whilst the user log out of power off the desktop? If yes, can a service also be launch at a specific user login?

Many thanks for your replies.

Offline

#2 2015-09-20 16:32:37

snakeroot
Member
Registered: 2012-10-06
Posts: 164

Re: systemd to launch script upon specific users?

Yes in both cases. Please review the systemd user article in the wiki.

As described in the article, to launch a service for all users, put the .service file in /etc/systemd/user/; for a service specific to a single user the .service file should be placed in ~/.config/systemd/user/.

The service can then be enabled with systemctl --user enable sweetthedevils.service or so.

Regards,

Offline

#3 2015-09-20 17:41:49

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: systemd to launch script upon specific users?

snakeroot wrote:

to launch a service for all users, put the .service file in /etc/systemd/user/

Small clarification: this is not to launch (that's what enable/start does), but simply to make the .service file available to all users.

Offline

#4 2015-09-20 18:53:25

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: systemd to launch script upon specific users?

Right, the question was that systemd will automatically launch the script when the user login - sorry I didn't read the page yet.

Is that not the case?

Offline

#5 2015-09-20 19:56:12

snakeroot
Member
Registered: 2012-10-06
Posts: 164

Re: systemd to launch script upon specific users?

Raynman wrote:

Small clarification: this is not to launch (that's what enable/start does), but simply to make the .service file available to all users.

Sorry, should have proofed my answer. You're right of course.

sweetthedevil wrote:

systemd will automatically launch the script when the user login

It can do so on first login; I don't think it will do so if you're already logged in and start another session on the same machine.

Just create a .service file and enable it:

[Unit]
Description=Sweetthedevil Login Service

[Service]
ExecStart=/path/to/sweetthedevilscript.sh

[Install]
WantedBy=default.target

Drop in directory per above and enable. It will then run on next first login. I just tested this and it seems to work.

Offline

#6 2015-09-20 20:01:26

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: systemd to launch script upon specific users?

anks for the answer.

My goal was for the script - which is a repetitive loop willterminate if the user either log out or power off the computer. If it doesn't achieve the above is there a better way?

Offline

#7 2015-09-20 20:04:00

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: systemd to launch script upon specific users?

Providing you write the unit file correctly then yes.
All user services will be stopped when the user no longer has any active sessions (unless they have enabled lingering).

All of this is covered in the wiki and the systemd man pages.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#8 2015-09-20 20:04:35

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,563
Website

Re: systemd to launch script upon specific users?

sweetthdevil wrote:

sorry I didn't read the page yet.

So go read it.  Do not expect to be spoon fed here.  Everything you described indicates that a systemd user service is what you want.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2015-09-21 19:42:20

sweetthdevil
Member
Registered: 2009-10-20
Posts: 417

Re: systemd to launch script upon specific users?

Hello,

You are right - I mentioned it because I did not have time, not that I didn't want to. It does make absolutle sense now that I read it.

However, I have tried to run squid.service with systemd --user but I am running into rights issues.

Is it not possible to run service like squid (web proxy) through systemd/user?

Offline

#10 2015-09-21 19:52:29

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: systemd to launch script upon specific users?

sweetthdevil wrote:

Is it not possible to run service like squid (web proxy) through systemd/user?

It is possible, but you would have to create a service file yourself as the one provided is for running it as a system service.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

Board footer

Powered by FluxBB