View previous topic :: View next topic |
Author |
Message |
waterloo2005 Apprentice
Joined: 26 Aug 2008 Posts: 271
|
Posted: Sat Jan 10, 2015 8:20 am Post subject: How to auto start script as root under systemd ? |
|
|
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 |
|
|
mv Watchman
Joined: 20 Apr 2005 Posts: 6747
|
Posted: Sat Jan 10, 2015 9:18 am Post subject: |
|
|
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 |
|
|
waterloo2005 Apprentice
Joined: 26 Aug 2008 Posts: 271
|
Posted: Tue Jan 13, 2015 3:10 am Post subject: |
|
|
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 |
|
|
evetsnameloc n00b
Joined: 14 Dec 2014 Posts: 26 Location: Colorado Springs, CO
|
Posted: Fri Jan 16, 2015 5:31 pm Post subject: |
|
|
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 |
|
|
waterloo2005 Apprentice
Joined: 26 Aug 2008 Posts: 271
|
Posted: Sat Jan 17, 2015 2:48 pm Post subject: |
|
|
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 |
|
|
|
|
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
|
|