Artix Linux Forum

General Category => Off-Topic => Topic started by: robto on 07 October 2022, 14:27:14

Title: get init system name by command
Post by: robto on 07 October 2022, 14:27:14
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.
Title: Re: get init system name by command
Post by: Dudemanguy on 07 October 2022, 16:24:23
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.
Title: Re: get init system name by command
Post by: Lancia on 07 October 2022, 17:39:36
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.
Title: Re: get init system name by command
Post by: Hitman on 07 October 2022, 18:34:51
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
Title: Re: get init system name by command
Post by: robto on 08 October 2022, 04:06:58
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
Title: Re: get init system name by command
Post by: robto on 10 October 2022, 11:01:03
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