Skip to main content
Topic: get init system name by command (Read 1177 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

get init system name by command

HI, i created a shell script to automate the setting of new linux installations for my personal usage. However im stuck at this init name . This is because I sometimes also use Arch which uses systemd. OpenRC and systemd are two systems i use. some programs like firewalld and others have scripts for openrc and systemd like firewalld-openrc ( which is not for systemd) and i want to get the init system name so that the script will auto install specified programs for detected init system.

Re: get init system name by command

Reply #1
You could use a find command for this and check to see what init binary you have on your system. i.e. 'find /usr/bin -name "openrc-init"' should only return something on an openrc system. I'm sure someone else can come up with some super clever one-liner or something, but you get the idea.

Re: get init system name by command

Reply #2
You could use a find command for this and check to see what init binary you have on your system. i.e. 'find /usr/bin -name "openrc-init"' should only return something on an openrc system. I'm sure someone else can come up with some super clever one-liner or something, but you get the idea.
An even simpler way I can think of is just seeing what /sbin/init points to.

Re: get init system name by command

Reply #3
I have devised a solution for this a while ago in my script, not pretty but it does it's job

The regex can be adjusted to include more, or possibly to match whole words (something like \s[a-zA-Z]{2,10})

Quote
init=$(strings /sbin/init | grep -oP 'OpenRC|Dinit|runit|s6|suite66' -m1) # determine init

Re: get init system name by command

Reply #4
Thanks all. Both of them worked. I also know about inxi ( inxi -Ix ) also get the init system the easiest way but inxi is not installed in by default in all OS. so i asked... Thanks again

 

Re: get init system name by command

Reply #5
I have devised a solution for this a while ago in my script, not pretty but it does it's job

The regex can be adjusted to include more, or possibly to match whole words (something like \s[a-zA-Z]{2,10})

Hey, I just got an idea while working with python ... so i wanted to share with you also. and this is for all in general.
so look.
we want to find if a command exists or not. the simple and easiest way is to use
Code: [Select]
which openrc
or  any other command in general like sudo , doas , any. and then check if the command exited with code 0 or not. so if command exited with code 0 then it is available and if not then we will consider that its not available and will do something else. I usually got this idea for identifying whether os has sudo or doas. so i did
Code: [Select]
which sudo
and compare if result code is 0 then
Code: [Select]
sudo pacman -Syu
and if not then
Code: [Select]
doas pacman -Syu