How to create custom options of my own in systemctl Linux?

Hello All,

Greetings!!

I was trying to create a custom option in systemctl like its out of the box options eg--> status, stop, start, restart , I have tried searching in google but didn't find anything related to it.

Basically I would like to create a custom option which is related to application like:

sudo systemctl app-activity service_name

Anyone have any thought is greatly welcomed and appreciated here.

Thanks,
R. Singh

As a core RedHat example, you can follow the directions in this link:

10.6. CREATING AND MODIFYING SYSTEMD UNIT FILES

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_files

Hello Neo,

Thank you for sharing the link, I am aware of creating systemd scripts with multiple options; I had created multiple of them. My question is related to options while running them. Like we have 4 major options which we can use with systemctl eg--> start, stop, status and restart. My query is how could I make a new/custom option related to my application? (For example clear cache of do some operations with this new option, I know I can call my python script inside systemd script to perform operations but thing is how to make a new option of it?) I hope I am clear now Neo.

Thanks,
R. Singh

Hi Ravinder,

Will look more carefully when I am back at my desk. Now I am up country in Thailand near Laos in the green forests with slow Internet on mobile.

In the meantime, maybe someone who works as a Linux sys admin can assist?

1 Like

I'm not at all sure that you can create a new option to systemctl and I've never attempted it in all my time working on kernel internals.

However, there are a vast number of things you can do with systemctl on a typical Linux distribution to make managing your applications easier. I guess that you know that there are a great number of options other that the 4 you list: status, start, stop & restart on a typical systemctl implementation.

You can edit the actions of options to address what you want to happen to your applications management.

I'm not even sure that what I've written here is any help to you at all!!! Perhaps you could elaborate on detail.

Refs:
How To Use Systemctl to Manage Systemd Services and Units | DigitalOcean
Understanding Systemd Units and Unit Files | DigitalOcean

I do not think init system is a place for such application features.
You can customize execution of code on those actions but .. your application should implement socket / network / database interface to add functionality or operation during runtime ?

Do you require a daemon type application doing stuff generally and occasionally execute some other code path on user input ?

Regards
Peasant.

Thank you Peasant and Dennis for your replies. I have created systemctl scripts(which are eg--> stop, start applications, call specific python, shell, perl programs in them too). So lets say 1 of the script is doing restart of app along with cleaning its cache too so in spite of going by sudo systemctl start/stop/restart service_name I want to have custom operation name like sudo systemctl restart-with-cache-clean service_name etc so that we can differentiate between 2 operations one which is simple restart and one which has clear cache in it. I hope this is clear now; I have got good expereience in creating systemd scripts now, so no question how to create them as of now :slight_smile:

Will be grateful to you folks if you could guide in this one, cheers.

Thanks,
R. Singh

I think the place to look is in the systemd architecture if you want to enhance or modify systemctl .

Everything is possible, you just need to decide where in the architecture to make your desired changes.

Try looking into systemd and then decide where to approach this problem, for example (sorry, I'm running out the door an don't have the time to do more research into this today):

Understanding and Using Systemd | Linux.com | The source for Linux information

1 Like

FYI

Here is the systemd source code on github:

1 Like

Ravinder,

If it was me, I would download the source code from github and search the source for these kinds of tokens, for example: start/stop/restart and try to find out where in the code these options are processed.

Then, you might find that it's not too difficult of a task to add a custom option to systemd by writing some code and recompiling your custom version of systemd . You might be able to link your new code in dynamically as a "ravinder" module.

The best way to do this, of course, is to fork the systemd code into your own github account, so you can track the changes and perhaps if your changes are interesting to others, they can download from your github page(s) as well. VSC has all the hooks you need to track all changes and manage all version control in the appropriate programming language (I assume C or C++, but have not checked).

I've not looked at the code, but I think it's a viable approach if you want to customize systemd , obviously you will need to modify the source, track changes, etc. and all the source is available.

It's just a matter of "doing it"... after doing your homework a bit more and deciding on where in the architecture to add your custom code.

1 Like