You are not logged in.

#1 2013-04-03 09:24:22

chord
Member
Registered: 2012-11-07
Posts: 121

[Solved] Add custom script to systemd

Hi !

Could be bash script added to systemd? Or should it be binary file?
I've created new service file and put him into /usr/lib/systemd/system. Also I've created /etc/systemd/system/Samba-umount.wants directory and put a simlink to '/usr/lib/systemd/system/Samba-umount.service' here.
The problem is that I can't start my Samba-umount. When tried to start it I got an error:

[root@arch archer]# systemctl start Samba-umount.service 
Job for Samba-umount.service failed. See 'systemctl status Samba-umount.service' and 'journalctl -xn' for details.
[root@arch archer]# 

Here  journalctl -xn output:

[root@arch archer]# journalctl -xn
Apr 03 12:59:12 arch systemd[1]: Starting Umount samba shares...
-- Subject: Unit Samba-umount.service has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- Documentation: http://www.freedesktop.org/wiki/Software/systemd/catalog/7d4958e842da4a758f6c1cdc7b36dcc5
-- 
-- Unit Samba-umount.service has begun starting up.
Apr 03 12:59:12 arch systemd[964]: Failed at step EXEC spawning /usr/lib/systemd/scripts/umount.sh: Exec format error
-- Subject: Process /usr/lib/systemd/scripts/umount.sh could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- Documentation: http://www.freedesktop.org/wiki/Software/systemd/catalog/641257651c1b4ec9a8624d7a40a9e1e7
-- 
-- The process /usr/lib/systemd/scripts/umount.sh could not be executed and failed.
-- 
-- The error number returned while executing this process is 8.
Apr 03 12:59:12 arch systemd[1]: Samba-umount.service: main process exited, code=exited, status=203/EXEC
Apr 03 12:59:12 arch systemd[1]: Failed to start Umount samba shares.
-- Subject: Unit Samba-umount.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- Documentation: http://www.freedesktop.org/wiki/Software/systemd/catalog/be02cf6855d2428ba40df7e9d022f03d
-- 
-- Unit Samba-umount.service has failed.
-- 
-- The result is failed.
Apr 03 12:59:12 arch systemd[1]: Unit Samba-umount.service entered failed state

Why script could not be executed? Script works fine when I starting it manually.
Here my script  (/usr/lib/systemd/scripts/umount.sh):

#!/bin/bash
 
umount -f -l /mnt/share
umount -f -l /mnt/torr

and service file (/usr/lib/systemd/system/Samba-umount.service):

[Unit]
Description=Umount samba shares
DefaultDependencies=no
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/umount.sh
StandardOutput=syslog

[Install]
WantedBy=multi-user.target

What's wrong here?

Thanks in advance.

Last edited by chord (2013-04-03 22:01:36)

Offline

#2 2013-04-03 10:34:17

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: [Solved] Add custom script to systemd

I've created new service file and put him into /usr/lib/systemd/system.

Custom service files belong in /etc/systemd/system.

Also I've created /etc/systemd/system/Samba-umount.wants directory and put a simlink to '/usr/lib/systemd/system/Samba-umount.service' here.

That doesn't make any sense.

From the journal it looks like it cannot execute your script (missing execute permissions?). systemctl status <unit> would have helped more.

It would help even more if you would explain what you are actually trying to do.  Do you want to start this on shutdown, and if so, why? systemd should unmount all shares automatically.

Offline

#3 2013-04-03 10:58:29

chord
Member
Registered: 2012-11-07
Posts: 121

Re: [Solved] Add custom script to systemd

Thank for reply!
Yes, I want to run this script on shutdown/reboot, because systemd can't umnount share and my box hangs on every reboot/shutdown. Just hangs in 'Umount /mnt/torr' stage. So I should reset my box by hardware reset button.

Script works just fine when I starting it by hand from terminal. Chmod +x was did also.

Here systemctl status output, nothing helpful found (at least for me):

[root@arch archer]# systemctl start Samba-umount.service 
Job for Samba-umount.service failed. See 'systemctl status Samba-umount.service' and 'journalctl -xn' for details.
[root@arch archer]# systemctl status Samba-umount.service 
Samba-umount.service - Umount samba shares
	  Loaded: loaded (/usr/lib/systemd/system/Samba-umount.service; enabled)
	  Active: failed (Result: exit-code) since Wed 2013-04-03 14:43:46 MSK; 3s ago
	 Process: 693 ExecStart=/usr/lib/systemd/scripts/umount.sh (code=exited, status=203/EXEC)

Apr 03 14:43:46 arch systemd[1]: Starting Umount samba shares...
Apr 03 14:43:46 arch systemd[1]: Samba-umount.service: main process exited, code=exited, status=203/EXEC
Apr 03 14:43:46 arch systemd[1]: Failed to start Umount samba shares.
Apr 03 14:43:46 arch systemd[1]: Unit Samba-umount.service entered failed state

About service's location: All manuals says that service should be located in /usr/li/systemd/system.

Offline

#4 2013-04-03 11:12:36

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: [Solved] Add custom script to systemd

About service's location: All manuals says that service should be located in /usr/li/systemd/system.

What are "All manuals"? The systemd manuals certainly don't tell you to put custom service files into /usr/lib.

The only other explanation I have is that you have a typo somewhere and the path is incorrect. Also, why do you set StandardOutput=syslog?

Nevertheless you shouldn't even have to do this, how do you configure your network? If the network service properly orders itself before network.target, this should just work without any workarounds.

Also post your /etc/fstab.

Offline

#5 2013-04-03 12:09:48

chord
Member
Registered: 2012-11-07
Posts: 121

Re: [Solved] Add custom script to systemd

OK, service moved to /etc/systemd/system/ directory and removed 'StandardOutput=syslog' option. Also I recreated link to service (under /etc/systemd/system/Samba-umount.wants/ directory). I still getting the same error:

[root@arch archer]# systemctl start Samba-umount.service 
Job for Samba-umount.service failed. See 'systemctl status Samba-umount.service' and 'journalctl -xn' for details.
[root@arch archer]# systemctl status Samba-umount.service 
Samba-umount.service - Umount samba shares
	  Loaded: loaded (/etc/systemd/system/Samba-umount.service; enabled)
	  Active: failed (Result: exit-code) since Wed 2013-04-03 15:56:16 MSK; 2s ago
	 Process: 680 ExecStart=/usr/lib/systemd/scripts/umount.sh (code=exited, status=203/EXEC)

Apr 03 15:56:16 arch systemd[1]: Starting Umount samba shares...
Apr 03 15:56:16 arch systemd[1]: Samba-umount.service: main process exited, code=exited, status=203/EXEC
Apr 03 15:56:16 arch systemd[1]: Failed to start Umount samba shares.
Apr 03 15:56:16 arch systemd[1]: Unit Samba-umount.service entered failed state

networking configured properly and works fine.

fstab:

/dev/sda1	/	ext4	defaults		0	0

also I tried to put into fstab cifs mount point - the same error.
Currently I mounting shares by running script manually, not via fstab

Offline

#6 2013-04-03 14:29:52

chord
Member
Registered: 2012-11-07
Posts: 121

Re: [Solved] Add custom script to systemd

I found solution:

#!/bin/bash

should be:

#!/bin/bash -

So now I can start service manually. If shares are mounted, service unmounting them (status=0/SUCCESS). If not, fails with message 'not mounted'. Manual start seems OK.
Now another trouble: service automatically starting on boot instead of shutdown, I thought 'Before=shutdown.target' should start it on shutdown, not on boot.
Why service starts on boot?
Any idea?

Offline

#7 2013-04-03 22:01:15

chord
Member
Registered: 2012-11-07
Posts: 121

Re: [Solved] Add custom script to systemd

Solved by changing wantby parameter.
Thanks for help, guys!

Offline

#8 2013-04-22 18:49:54

brando56894
Member
From: NYC
Registered: 2008-08-03
Posts: 681

Re: [Solved] Add custom script to systemd

Can you post the corrected file? I'm having this issue also, I'll add it to the wiki also.

Offline

#9 2013-04-22 19:09:34

chord
Member
Registered: 2012-11-07
Posts: 121

Re: [Solved] Add custom script to systemd

brando56894 wrote:

Can you post the corrected file? I'm having this issue also, I'll add it to the wiki also.

unfortunately I removed this service. One thing I do remember: WantedBy=multi-user.target is wrong. As far as I remember I combined a three or four targets under WantedBy parameter. It was something like:
WantedBy=reboot.target shutdown.target halt.target

Offline

#10 2013-04-22 19:10:40

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

Re: [Solved] Add custom script to systemd

chord wrote:

Also I recreated link to service (under /etc/systemd/system/Samba-umount.wants/ directory).

As 65kid said, that doesn't make any sense. That directory name doesn't match systemd's naming scheme, but if it did you would be adding a recursive dependency, because you're making the service depend on itself hmm

As man page says, probably best to not manually create such symlinks unless you know what you're doing.

Offline

#11 2013-04-22 19:53:28

brando56894
Member
From: NYC
Registered: 2008-08-03
Posts: 681

Re: [Solved] Add custom script to systemd

chord wrote:
brando56894 wrote:

Can you post the corrected file? I'm having this issue also, I'll add it to the wiki also.

unfortunately I removed this service. One thing I do remember: WantedBy=multi-user.target is wrong. As far as I remember I combined a three or four targets under WantedBy parameter. It was something like:
WantedBy=reboot.target shutdown.target halt.target

Thanks, I'll give it a go but it's not really a prime concern right now since I have two computers side by side (one is Windows 7 and the other is Arch Linux). I'm trying to get everything on the Linux box (W7 via virtualbox in seamless mode) and I can do what I need on the W7 box for the time being and just remove the share from fstab for the time being.

My main goal right now is to try to get dual NICs to route traffic to different subnets and it's proving to be a challenge for me and others. If anyone wants to take a look at it, here's the thread: https://bbs.archlinux.org/viewtopic.php … 1#p1262421

Last edited by brando56894 (2013-04-22 19:56:01)

Offline

#12 2017-03-06 15:34:42

GNUtoo
Member
Registered: 2017-03-06
Posts: 1

Re: [Solved] Add custom script to systemd

As the person who solved it ran away without posting a correct solution, and that I had an issue with the same symptom, I'll share the solution.

If the script that you try to run:
- Works when you run it manually
- Has the right permission (755)
- Produce some "Exec format error" like that:

[...] systemd[5196]: battery-log@resume.service: Failed at step EXEC spawning /usr/local/bin/batt.sh: Exec format error
[...] systemd[1]: Starting After suspend (at resume), log the battery percentage...
[...] systemd[1]: battery-log@resume.service: Main process exited, code=exited, status=203/EXEC
[...] systemd[1]: Failed to start After suspend (at resume), log the battery percentage.
[...] systemd[1]: battery-log@resume.service: Unit entered failed state.
[...] systemd[1]: battery-log@resume.service: Failed with result 'exit-code'.
[...] systemd[1]: Starting After suspend (at resume), log the battery percentage...

It might be that the script has no shell set on the first line.
If your script has some comments before the sheebang(something like "#!/bin/sh") or lacks it entierly, it work perfectly fine when run by hand but produce the error pasted above.

Since the same same symptom can have very different causes, this only shows one of the ways this can happen.
For instance if bash cannot be executed, it might produce a similar error message (or not).

Denis.

Offline

#13 2017-03-06 15:43:50

brando56894
Member
From: NYC
Registered: 2008-08-03
Posts: 681

Re: [Solved] Add custom script to systemd

Please don't revive long dead threads, this one hasn't had a response for 3.5 years....

Offline

#14 2017-03-06 20:34:46

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,859
Website

Re: [Solved] Add custom script to systemd

Thanks for sharing your findings. As noted, this is an old topic, so I'm going to go ahead and close it now.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

Board footer

Powered by FluxBB