The officially official Devuan Forum!

You are not logged in.

#1 2024-06-13 19:26:01

webman
Member
Registered: 2022-08-27
Posts: 45  

sysv-init: Looking for example/run a script permanently, like service?

Hello All!
Just a very simple script and where and how to install it would help
me getting started!

Thanks,
Manfred

Offline

#2 2024-06-16 03:12:40

EDX-0
Member
Registered: 2020-12-12
Posts: 76  

Re: sysv-init: Looking for example/run a script permanently, like service?

take a look at https://github.com/eylles/afreq.sh it is just a shell script (posxi shell, not even bash) along the initscript to run it as a daemon.

Offline

#3 2024-06-19 21:52:00

webman
Member
Registered: 2022-08-27
Posts: 45  

Re: sysv-init: Looking for example/run a script permanently, like service?

Thank you very much!
--br,
Manfred

Offline

#4 2024-06-24 03:28:07

Eeqmcsq
Member
Registered: 2017-09-19
Posts: 21  

Re: sysv-init: Looking for example/run a script permanently, like service?

I also need an init script so that I can launch the x11vnc server without a user logging in. I broke up the task into two subtasks:

  - Figure out how to set up the init script, how it launches, what args it receives.
  - Write the logic for starting and shutting down the x11vnc server.

Today, I figured out some of the basics for writing an init script, starting it etc. Next time I have free time, I'll figure out how to start and stop the vnc server from the init script.

These steps were tested in Devuan Daedalus, with the MATE desktop environment.

* Create the script file

The init scripts are located in /etc/init.d. In this example, we'll create a script that will log all of the script args into a log file.

sudo pluma /etc/init.d/yourinitscript.sh

Put these contents in the file.

#!/bin/sh

#If you modify the INIT INFO section, update the rc.d links with these commands (as root):
#  update-rc.d yourinitscript.sh remove
#  update-rc.d yourinitscript.sh defaults
### BEGIN INIT INFO
# Provides: yourinitscript.sh
# Required-Start:
# Required-Stop:
# Default-Start: 1 2 3 4 5
# Default-Stop: 0 6
### END INIT INFO

#Write the current date/time, previous and current run levels, and the args passed into this script into the log file.
echo "`date` | runlevel `runlevel` | script args received: $*" >> /var/tmp/yourinitscript.log

Sysvinit uses the INIT INFO section to figure out the dependencies and what order each init script should be launched. These five appear to be the minimum required params that an init script needs to define.
  - Provides : I think sysvinit uses this to identify your script when it figures out the dependencies.
  - Required-Start: This defines what other init scripts must be run before telling yourinitscript.sh to start its service. In this example, we don't have any dependencies, so we leave it blank.
  - Required-Stop: Other init scripts that must be stopped before telling yourinitscript.sh to stop its service.
  - Default-Start: When sysvinit is running at these run levels, yourinitscript.sh will be started if it's not started.
  - Default-Stop: When sysvinit is running at these run levels, yourinitscript.sh will be stopped if it's started.

* Set up the symlinks

First, the script must be executable.

sudo chmod +x /etc/init.d/yourinitscript.sh

Next, we tell sysvinit to set up the symlinks that will run yourinitscript.sh whenever the services needs to be started or stopped.

sudo update-rc.d yourinitscript.sh remove
sudo update-rc.d yourinitscript.sh defaults

The symlinks are created in the subdirectores for each run level: /etc/rc0.d, /etc/rc1.d, /etc/rc2.d, etc.

I haven't figuured out why, but if you modify the script's INIT INFO section and then run "update-rc.d defaults", the command will give warnings. The workaround is to always "remove" any previously setups of yourinitscript.sh, then set up it with "defaults".

* Test the init script

To test that the script is working correctly, use the "service" command. You can pass in any args into your init script.

sudo service yourinitscript.sh aaa bbb ccc

Read the log file.

cat /var/tmp/yourinitscript.log

You should see this line in the log file.

Sun Jun 23 07:32:18 PM PDT 2024 | runlevel N 2 | script args received: aaa bbb ccc

Notice that the previous run level is "N", which means there's no previous run level. The current run level is "2", which is defined in /etc/inittab.

# The default runlevel.
id:2:initdefault:

Also notice that the script received the 3 dummy args "aaa", "bbb", and "ccc". This means that if necessary, your init script can receive all sorts of args. That is, it's not restricted to a specific set of keywords.

* Reboot the PC

Reboot the PC, then check the log file.

cat /var/tmp/yourinitscript.log

There should be two new lines.

Sun Jun 23 19:33:58 PDT 2024 | runlevel 2 6 | script args received: stop
Sun Jun 23 19:34:26 PDT 2024 | runlevel N 2 | script args received: start

Notice that on the first line, the previous run level is "2" and the current runlevel is "6". "6" is the runlevel that means the PC is rebooting.

Also notice that the script received the "stop" arg when the PC was shutting down for a reboot, and the "start" arg during the next boot. So your init script should at least handle these two args.

* Shutdown, then manually power up again

Shut down the PC. Then turn it back on.

cat /var/tmp/yourinitscript.log

There should be two new lines.

Sun Jun 23 19:45:27 PDT 2024 | runlevel 2 0 | script args received: stop
Sun Jun 23 19:46:04 PDT 2024 | runlevel N 2 | script args received: start

Notice that during the shutdown, the current runlevel is "0". "0" is the runlevel that means the PC is shutting down (halting).

* Uninstall

To uninstall the init script, first remove the symlinks.

sudo update-rc.d yourinitscript.sh remove

Optionally, delete the script. Make sure you have a backup copy of this script so you can install it again if needed!

sudo rm /etc/init.d/yourinitscript.sh

Hopefully, these steps are helpful to someone.

Offline

Board footer