Artix Linux Forum

Init systems => S6 => Topic started by: Gh0st on 02 February 2024, 20:46:11

Title: Help me write my first s6 script
Post by: Gh0st on 02 February 2024, 20:46:11
Hey. I'd like to test out s6, and part of it for me would be transferring crucial service files into s6's scripting language to be able to continue using these tools. I really can't find a proper explanation anywhere for how these are written, though.

Could you guys firstly point out to me where I can find some resources that can properly teach me the way s6's services work, properly? Throw me a man page if you can, I've spent a few minutes looking at it and can't figure out which holds the information I need.

And in addition to that; maybe show me a practical example? Here's the service of one of my tools, could you help me port it over?

Code: [Select]
[Unit] 
Description=Neo4j Management Service

[Service]
Type=forking
User=neo4j
RuntimeDirectory=neo4j
RuntimeDirectoryMode=770
ExecStart=/usr/bin/neo4j start
ExecStop=/usr/bin/neo4j stop
ExecReload=/usr/bin/neo4j restart
RemainAfterExit=no
Restart=on-failure
PIDFile=/var/run/neo4j/neo4j.pid
LimitNOFILE=60000
TimeoutSec=600

[Install]
WantedBy=multi-user.target

Title: Re: Help me write my first s6 script
Post by: Ogis on 03 February 2024, 12:58:38
Hello.

Check this (https://wiki.artixlinux.org/Main/S6#Basic_usage) and this (https://wiki.artixlinux.org/Main/LocalUserServicesOns6).

Good luck.
Title: Re: Help me write my first s6 script
Post by: Gh0st on 03 February 2024, 13:35:28
Hello.

Check this (https://wiki.artixlinux.org/Main/S6#Basic_usage) and this (https://wiki.artixlinux.org/Main/LocalUserServicesOns6).

Good luck.

Excellent! That's just what I was looking for, resource-wise. I found some additional papers and man pages in the meantime too, but this'll be added to it. Thank you!
Title: Re: Help me write my first s6 script
Post by: capezotte on 19 May 2024, 03:43:03
Kind of necrobumping, but I really hope this might be helpful.



This isn't the best case scenario for "my first s6 script" because of Type=forking — s6 works best with what systemd calls simple, exec or oneshot services.

Nevertheless, let's read the file line by line:

With what we've said so far, the script might look like this:

Code: [Select]
#!/bin/sh -e
# install -d = create a folder...
# -m770 = ...with permissions 770...
# -o neo4j -g neo4j = ...owned by user and group neo4j.
install -d -m770 -o neo4j -g neo4j /run/neo4j
# order is important! s6-setuidgid must be last thing we call.
exec s6-softlimit -o 60000 s6-setuidgid neo4j ...

Here's where it gets a bit messy. As I said, forking is not the best type for a s6 service, because they needlessly go to the background (https://jdebp.uk/FGA/unix-daemon-design-mistakes-to-avoid.html) and s6 loses track of them. The classic way to work around that is to have s6-fghack before the final program (which will keep track of the backgrounded process instead of s6 itself), and create a finish script with the contents of ExecStop:

Code: [Select]
run
=========
#!/bin/sh -e
install -d -m770 -o neo4j -g neo4j /run/neo4j
exec s6-softlimit -o 60000 s6-fghack s6-setuidgid neo4j /usr/bin/neo4j start

Code: [Select]
finish
=========
#!/bin/sh -e
/usr/bin/neo4j stop

The problem is, when s6-fghack is necessary, s6-svc can only stop the process. To send any other signals, you must run kill -s SIGNAL $(cat /var/run/neo4j.pid) manually (notice we're using the contents of the PIDFile variable in this command.

There's a little bit of hope: with these Java programs, usually the thing that's given in unit files as a ExecStart/ExecStop is a shell script that just calls Java with arguments that make them go to background. If that's the case, you can try analyzing it and making a new script that doesn't background and does not use PID files. I wish I could analyze it and give the answer here, but the program requires registration to download the package :/.

Here's an example (http://jdebp.uk/FGA/systemd-house-of-horror/tomcat.html) where a Java program was converted from one such shell script into a Type=simple systemd unit; in that case, you'd just copy-paste the ExecStart after s6-setuidgid, you'd not need s6-fghack and you'd have the full power of s6-svc.