You are not logged in.

#1 2012-09-01 23:57:31

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

[Solved] systemd services to replace /etc/rc.local{,.shutdown}

EDIT:

Okay I admit it. I did something terrible. I plagiarized the rc-local and re-local-shutdown services from initscripts 2012.08.3-2. All I had to do was add the [Install] sections to both to get them to enable and they work perfectly on my non-sysvinit exclusively systemd install. You can take a look at my script [Solved] Install script for system encrypted pure UEFI/systemd Arch post to see how I installed Arch using the latest Archboot media.


Ordering your scripts can be as simple as listing them in the order you want them executed using multiple ExecStart= directives but only if you use the Type=oneshot option. Note the minus sign '-' before the script path/name. This is necessary because without it should a script fail none of the following ones will get executed and the service will exit prematurely. And if you need to be sure the network is up and running before your run your scripts just add After=network.target to the Unit section.

Now the real fun begins as I model my scripts after these to convert them all to systemd and stop using /etc/rc.local{,-shutdown} completely.

So here they are. The slightly modified plagiarized versions...

/usr/lib/systemd/system/rc-local.service

[Unit]
Description=/etc/rc.local Compatibility
After=network.target

[Service]
Type=oneshot
ExecStart=-/etc/rc.local
ExecStart=-/pathtoyour/script1
ExecStart=-/pathtoyour/script2
TimeoutSec=0
StandardInput=tty
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

/usr/lib/systemd/system/rc-local-shutdown.service

[Unit]
Description=/etc/rc.local.shutdown Compatibility
ConditionFileIsExecutable=/etc/rc.local.shutdown
DefaultDependencies=no
After=rc-local.service basic.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/etc/rc.local.shutdown
StandardInput=tty
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target


Have fun!

Haven't been able to find any workable rc.local and rc.local.shutdown style substitutes for systemd.

The rc.local replacement needs the network to be up and running.

They both need to be able to accept user input.

Nothing fancy but still I can't seem to find anything. Got as far as having the script appear when I boot but it either locks up from the get-go or only allows one character to be entered before it locks up.

Last edited by KairiTech (2012-09-08 11:48:07)

Offline

#2 2012-09-02 00:01:35

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

rc-local.service

[unit]
Description=/etc/rc.local Compatibility

[Service]
Type=oneshot
ExecStart=/etc/rc.local
TimeoutSec=0
StandardInput=tty
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Also=rc-local-shutdown.service

rc-local-shutdown.service

[Unit]
Description=/etc/rc.local.shutdown Compatibility
After=rc-local.service

[Service]
Type=oneshot
ExecStart=/etc/rc.local.shutdown
StandardInput=tty
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target
Also=rc-local.service

You know I was able to find this in just a couple minutes perusing the wiki...

Offline

#3 2012-09-02 02:09:43

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

Thanks. I was missing 'RemainAfterExit=yes' in rc-local.service.

No sign of rc-local-shutdown.service when I shutdown though.

Offline

#4 2012-09-02 02:39:39

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

Then use ExecStop=/etc/rc.local.shutdown

Or really, don't use rc.local. It's an ugly hack and there's always a better way.

Offline

#5 2012-09-02 03:27:36

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

'ExecStop=/etc/rc.local.shutdown' didn't seem to make a difference.

By "don't use" do you mean not to run any custom scripts at boot or shutdown?

Or do you mean do not call /etc/rc.local or /etc/rc.local.shutdown from the service but instead call my custom script directly? "A rose by any other name..." and all that. Though I suspect it's more about a sow's ear and a purse.

Here's one of the scripts I want to run on boot:

if [[ ! `ping -c1 google.ca 2>&1 | grep unknown` ]]
   then
      MESSAGE="Synchronizing time..." ; NOTIFY="INFO" ; . /opt/myArch/notify.sh
      y=`{ ntpd -qg; hwclock -w ; }`
      echo ${y}
      logger "myArch: ${y}"
   else
      echo "Yikes! No network."
      logger "myArch: Network NOT available for time Synchronization"
fi

Last edited by KairiTech (2012-09-02 03:52:54)

Offline

#6 2012-09-02 03:50:28

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

I think the preferred (yet still considered a hack) method is to put custom scripts in /usr/lib/systemd/system-shutdown... there is also a system-sleep, which is where I set up netcfg to reconnect after wake.


Edit: I'm curious, what is the advantage of your custom script for time synchronization?  I see that you are ultimately using ntpd, but in a very round-about way... not saying it is wrong, but I am just wondering...

Last edited by WonderWoofy (2012-09-02 03:52:52)

Offline

#7 2012-09-02 03:55:12

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

KairiTech wrote:

'ExecStop=/etc/rc.local.shutdown' didn't seem to make a difference.

By "don't use" do you mean not to run any custom scripts at boot or shutdown?

No, I mean that there's a better way.

KairiTech wrote:

Here's one of the scripts I want to run on boot:

if [[ ! `ping -c1 google.ca 2>&1 | grep unknown` ]]
   then
      MESSAGE="Synchronizing time..." ; NOTIFY="INFO" ; . /opt/myArch/notify.sh
      y=`{ ntpd -qg; hwclock -w ; }`
      echo ${y}
      logger "myArch: ${y}"
   else
   	echo "Yikes! No network."
      logger "myArch: Network NOT available for time Synchronization"
fi

You need absolutely none of this. This is what ntpd.service does on its own. It'll wait for the network to come up before starting (and doing an initial sync with --panicgate).

Offline

#8 2012-09-02 03:57:52

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

falconindy wrote:
KairiTech wrote:

Here's one of the scripts I want to run on boot:

if [[ ! `ping -c1 google.ca 2>&1 | grep unknown` ]]
   then
      MESSAGE="Synchronizing time..." ; NOTIFY="INFO" ; . /opt/myArch/notify.sh
      y=`{ ntpd -qg; hwclock -w ; }`
      echo ${y}
      logger "myArch: ${y}"
   else
   	echo "Yikes! No network."
      logger "myArch: Network NOT available for time Synchronization"
fi

You need absolutely none of this. This is what ntpd.service does on its own. It'll wait for the network to come up before starting (and doing an initial sync with --panicgate).

That is what I thought.... just thought the OP might be in on some secret I wasn't aware of.

Offline

#9 2012-09-02 04:00:47

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

WonderWoofy wrote:

I think the preferred (yet still considered a hack) method is to put custom scripts in /usr/lib/systemd/system-shutdown... there is also a system-sleep, which is where I set up netcfg to reconnect after wake.


Edit: I'm curious, what is the advantage of your custom script for time synchronization?  I see that you are ultimately using ntpd, but in a very round-about way... not saying it is wrong, but I am just wondering...

Perhaps ntpd was a poor example for what I'm trying to accomplish which is to run custom scripts (any custom script) at boot or shutdown.

But to the point...if it's a hack it's a hack..so be it. My question is what is (or is going to be) the acceptable alternative?

Offline

#10 2012-09-02 04:03:37

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

WonderWoofy wrote:
falconindy wrote:
KairiTech wrote:

Here's one of the scripts I want to run on boot:

if [[ ! `ping -c1 google.ca 2>&1 | grep unknown` ]]
   then
      MESSAGE="Synchronizing time..." ; NOTIFY="INFO" ; . /opt/myArch/notify.sh
      y=`{ ntpd -qg; hwclock -w ; }`
      echo ${y}
      logger "myArch: ${y}"
   else
   	echo "Yikes! No network."
      logger "myArch: Network NOT available for time Synchronization"
fi

You need absolutely none of this. This is what ntpd.service does on its own. It'll wait for the network to come up before starting (and doing an initial sync with --panicgate).

That is what I thought.... just thought the OP might be in on some secret I wasn't aware of.

ntpd was a bad example. Just think of any custom script that needs to be run at boot or shutdown.

Last edited by KairiTech (2012-09-02 04:04:30)

Offline

#11 2012-09-02 04:05:45

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

Ok... and my point is that there's a better way than just dropping it in a singular file and executing them in serial.

Offline

#12 2012-09-02 04:09:41

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

falconindy wrote:

Ok... and my point is that there's a better way than just dropping it in a singular file and executing them in serial.

So if I understand you correctly each custom script should have its own service?

Offline

#13 2012-09-02 04:41:24

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

I don't think he is necessarily telling you that it *should* be a service, but rather that having an all purpose scratchpad script like rc.local is messy and rather less than an elegant solution.  Hence support for it was dropped, seemingly in favor of making use of the tools that systemd provides (which is many).

Offline

#14 2012-09-02 10:25:25

replabrobin
Member
Registered: 2010-05-06
Posts: 226

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

I have another example of rc.local which is mainly about configuring power saving etc etc on a netbook

hdparm -B254 /dev/sda
grep -q '0' /sys/class/power_supply/ADP1/online && P="powersave" || P="ondemand"
cpupower -c all frequency-set -g "$P"
echo 1500 > /proc/sys/vm/dirty_writeback_centisecs
/usr/sbin/iw dev wlan0 set power_save on
echo '0' > '/proc/sys/kernel/nmi_watchdog'
echo '1' > '/sys/module/snd_hda_intel/parameters/power_save'
echo 'auto' > '/sys/bus/pci/devices/0000:00:00.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:02.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1c.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1b.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1d.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1d.1/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1d.2/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1d.3/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1d.7/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.2/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.3/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:01:00.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:02:00.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1c.1/power/control'
/usr/sbin/ethtool -s eth0 wol d

what is the systemd way of doing this sort of thing?

Also where should local units be put so that they can be enabled/disabled etc. If I put them in /usr/lib/systemd/system won't they interfere with pacman?

Offline

#15 2012-09-02 11:22:59

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

Hi,
Your power saving commands belong in a custom service with an appropriate name, the echo lines could also go in tempfiles.d or udev rules.
cpupower has its own service, the configuration is in /etc/conf.d/cpupower
Custom units belong in /etc/systemd/system

Last edited by progandy (2012-09-02 11:23:32)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Online

#16 2012-09-02 15:35:43

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

progandy wrote:

Hi,
Your power saving commands belong in a custom service with an appropriate name, the echo lines could also go in tempfiles.d or udev rules.
cpupower has its own service, the configuration is in /etc/conf.d/cpupower
Custom units belong in /etc/systemd/system

One thing I have been still trying to figure out is the hdparm.  I know that there must be a better solution than the ghetto/ half-@$$ed service file I made for it.  Also I don't think it is not persistent between suspends (which I can fix with systemd-sleep).  My first thought was to use hdparm.conf, but it seems that feature is not included in the arch package.

Offline

#17 2012-09-02 16:50:00

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

Getting back to the topic of my post:

So far I have what seems to be a working rc.local style service that I intend to use as a model to convert my scripts to systemd.

/usr/lib/systemd/system/rc-local.service

[unit]
Description="/etc/rc.local Compatibility"

[Service]
Type=oneshot
ExecStart=/etc/rc.local
TimeoutSec=0
StandardInput=tty
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Can't seem to get the rc.local.shutdown style service to work though.

/usr/lib/systemd/system/rc-local-shutdown.service

[Unit]
Description="/etc/rc.local.shutdown Compatibility"
After=rc-local.service

[Service]
Type=oneshot
ExecStart=/etc/rc.local.shutdown
StandardInput=tty
RemainAfterExit=yes

[Install]
WantedBy=shutdown.target

Last edited by KairiTech (2012-09-02 17:57:10)

Offline

#18 2012-09-02 22:15:11

replabrobin
Member
Registered: 2010-05-06
Posts: 226

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

One thing I noticed after using a copy of the above is that it complains about the spelling of 'unit'; it should be Unit. I don't think that can affect the shutdown service though.

Offline

#19 2012-09-03 13:32:58

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [Solved] systemd services to replace /etc/rc.local{,.shutdown}

replabrobin wrote:

One thing I noticed after using a copy of the above is that it complains about the spelling of 'unit'; it should be Unit. I don't think that can affect the shutdown service though.

I noticed that after the post but it is corrected now.

Offline

Board footer

Powered by FluxBB