Skip to content

Instantly share code, notes, and snippets.

@houtianze
Last active February 20, 2024 14:00
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save houtianze/59b89ca71cc2029dd61d3c4bd86e6ee7 to your computer and use it in GitHub Desktop.
Save houtianze/59b89ca71cc2029dd61d3c4bd86e6ee7 to your computer and use it in GitHub Desktop.
Convert SysV Init scripts to Systemd Unit File

Let's say you have a SysV Init Script named foo

  1. Copy the file to /etc/init.d/foo

  2. Enable the SysV service: chkconfig --add foo

  3. Enable the SysV service: chkconfig foo on

  4. Start the service: service foo start. After this, systemd-sysv-generator will generate this file /run/systemd/generator.late/foo.service, copy this file to /etc/systemd/system by running: cp /run/systemd/generator.late/foo.service /etc/systemd/system/foo.service

  5. Edit /etc/systemd/system/foo.service by running systemctl edit foo.service, add in the following line to foo.servie (this makes the service installable)

    [Install]
    WantedBy=multi-user.target
    
  6. Enable the service: systemd enable foo.service

  7. (Optional) You can then remove the SysV script by running chkconfig foo off && chkconfig --del foo

@Narimm
Copy link

Narimm commented Jan 19, 2018

I dont quite understand how this changes things...given that the systemd script will have a line
ExecStart=/etc/init.d/foo start
ExecStop=/etc/init.d/foo stop

You are still using the init.d script

@Narimm
Copy link

Narimm commented Jan 19, 2018

Additionally chkconfig is replaced by update-rc.d
on Ubuntu 16

@kamal-rathi-EMR
Copy link

kamal-rathi-EMR commented Jun 8, 2020

post is really good,it helped me a lot
issue is that now all processes in user.slice got mercilessly killed even if your services in sysVinit was desgined to run first when you initate reboot or shutdown.

I have tried to put After=user.slice in [unit] section and WantedBy=user.slice in [Install] but no luck

Any suggestion if i want my service to initiated before processes in user.slice killed.

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/dbtiershut
Description=LSB: stop ORACLE DB Tier Databases in /etc/instances
Before=runlevel2.target
Before=runlevel3.target
Before=runlevel4.target
Before=runlevel5.target
Before=shutdown.target
After=network-online.target
After=ohasd.service
After=splunk.service
Conflicts=shutdown.target
After=user.slice

[Service]
#Type=forking
Type=simple
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/dbtiershut start
ExecStop=/etc/rc.d/init.d/dbtiershut stop
[Install]
WantedBy=user.slice.target multi-user.target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment