Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
How to auto start script as root under systemd ?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
waterloo2005
Apprentice
Apprentice


Joined: 26 Aug 2008
Posts: 271

PostPosted: Sat Jan 10, 2015 8:20 am    Post subject: How to auto start script as root under systemd ? Reply with quote

I remember it can be done in local.start of openrc.

How to do it under systemd ?

Then how to auto run script as waking from suspend ?

Thanks
_________________
i5-2450M, gnome, amd64
R,Mathematica,Emacs
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6069

PostPosted: Sat Jan 10, 2015 9:18 am    Post subject: Reply with quote

It is one "design goal" of systemd to hinder you from running scripts.

Of course, it cannot hinder you to do it through a backdoor: Write something like such a unit
/etc/systemd/system/local-start-stop.service wrote:
[Unit]
Description=local start/stop script

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/sh /etc/init.d/local-start-stop.sh start
ExecStop=/bin/sh /etc/init.d/local-start-stop.sh stop

[Install]
WantedBy=multi-user.target
and make a corresponding script
/etc/init.d/local-start-stop.sh wrote:
#!/bin/sh
for file in /etc/local.d/*.${1}
do ! test -x "${file}" || "${file}"
done
After this enable the local-start-stop service. Concerning freeze, I would have to RTFM: Probably, there is a corresponding target, too.
Of course, you cannot be sure that your script is run after all services are started, because it can happen that there are services which are never started or only hours after booting.
Back to top
View user's profile Send private message
waterloo2005
Apprentice
Apprentice


Joined: 26 Aug 2008
Posts: 271

PostPosted: Tue Jan 13, 2015 3:10 am    Post subject: Reply with quote

mv wrote:
It is one "design goal" of systemd to hinder you from running scripts.

Of course, it cannot hinder you to do it through a backdoor: Write something like such a unit
/etc/systemd/system/local-start-stop.service wrote:
[Unit]
Description=local start/stop script

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/sh /etc/init.d/local-start-stop.sh start
ExecStop=/bin/sh /etc/init.d/local-start-stop.sh stop

[Install]
WantedBy=multi-user.target
and make a corresponding script
/etc/init.d/local-start-stop.sh wrote:
#!/bin/sh
for file in /etc/local.d/*.${1}
do ! test -x "${file}" || "${file}"
done
After this enable the local-start-stop service. Concerning freeze, I would have to RTFM: Probably, there is a corresponding target, too.
Of course, you cannot be sure that your script is run after all services are started, because it can happen that there are services which are never started or only hours after booting.


Thanks

Then how to auto run script as waking from suspend ?
_________________
i5-2450M, gnome, amd64
R,Mathematica,Emacs
Back to top
View user's profile Send private message
evetsnameloc
n00b
n00b


Joined: 14 Dec 2014
Posts: 26
Location: Colorado Springs, CO

PostPosted: Fri Jan 16, 2015 5:31 pm    Post subject: Reply with quote

Place the script you want to run in /usr/lib/systemd/system-sleep. Systemd will run scripts in this folder on both suspend and resume. It passes two arguments to the scripts, the first being either "pre" or "post" depending on whether it is suspending or resuming, the second being "suspend", "hibernate" or "hybrid-sleep". So if you want to run a script only on waking from suspend, you will want a script something like
Code:
#!/bin/bash

if [ "$1" == "post" ]; then

   <do this>
        
fi

exit 0
Back to top
View user's profile Send private message
waterloo2005
Apprentice
Apprentice


Joined: 26 Aug 2008
Posts: 271

PostPosted: Sat Jan 17, 2015 2:48 pm    Post subject: Reply with quote

evetsnameloc wrote:
Place the script you want to run in /usr/lib/systemd/system-sleep. Systemd will run scripts in this folder on both suspend and resume. It passes two arguments to the scripts, the first being either "pre" or "post" depending on whether it is suspending or resuming, the second being "suspend", "hibernate" or "hybrid-sleep". So if you want to run a script only on waking from suspend, you will want a script something like
Code:
#!/bin/bash

if [ "$1" == "post" ]; then

   <do this>
        
fi

exit 0


Thanks , it works .
_________________
i5-2450M, gnome, amd64
R,Mathematica,Emacs
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum