Set custom Process name for service in Linux CentOS

I have had created some systemd services in Linux(CentOS) which described at create-linux-systemd-service. Now for a monitoring requirement that's needed to set a custom process name to my running services.

Some different solutions were tested and none of them worked on CentOS-7x.

Finally founded that there is a very simple solution which I am going to share it here. As described at previous post about systemd we created a service file which has a ExecStart part in which I had directly run a Java process, for example it was something like this:

In this way CentOS is not going to assign a process name to the running process. You can test this by trying one of below commands:

ps aux | grep myservice
pgrep myservice

Believe it or not the only thing that is needed to assign a name to our process is moving the command in front of ExexStart into an sh file and tell ExecStart to run that file, in this way CentOS will assign the name of sh file into the process. Here are steps:

run command: sudo systemctl daemon-reload

run command: chmod 711 /home/keshavarzreza/bin/ run command: sudo systemctl restart myservice.service

Now you can try one of below commands again and this time you would see your process(es) PID(s) in response:

ps aux | grep myservice
pgrep myservice


create a Linux systemd service and make it runable for non-root users

We can easily delegate management of running a process/script into Linux by systemd. systemd is a Linux system tool that one of its features is bootstrapping start and manage system processes. For better understanding we are going to create a custom systemd service to make it easy to start, stop, restart our script as well as configure it to start at boot. And we make non-root users able to use these facilities.

1. Create a systemd service

Suppose that we have a script in /usr/bin/ Also suppose that there is a Linux user named myuser in group ourgroup Now this is the main part of the story in which we make a file in /etc/systemd/system/myservice.service including below directives: I'm not going to make this tutorial a long story by describing about parts of .service file. So let's find out how we can make sense about its usability. Run this command to make your myscipt.service loaded into systemd: Now as a sudoer user you are able to manage myscipt.service using systemctl commands: But how we make it possible for not-root users to use these facilities?!

2. Use PolicyKit to allow non-root users to mange the service

PolicyKit is an application-level toolkit for defining and handling the policy. As a practical step here we create a new file in /etc/polkit-1/rules.d/99-ourgroup.rules including:

3. Define needed sudoer access using visudo

Linux and Unix administrators use sudoers file to allocate system rights to system users. This allows the administrator to control who does what. Visudo command is a secure and safe way of editing the /etc/sudoers file on Linux. So just run $sudo visudo command and add below lines into sudoers file: