Jump to content

How to Create and Manage User Services on Linux


Linux Hint

Recommended Posts

A normal user of Linux can create a custom systemd service. While systemd services are largely managed by system administrators using the systemctl command. The same command can also be used to manage user-specific services; all you need is a –user flag.

In this guide, I will cover how to create a user-specific service on Linux and how to manage it using systemctl.

Reason for Having a Normal User Service

The normal user service is different from the system service. The normal user service is logged-in user-centric. This service will only work in the session of the user who created it.

How to Create a Normal User Service

The user-specific services on Linux are placed in the ~/.config/systemd/user directory. If this directory is not present, then it can be created.

mkdir -p ~/.config/systemd/user

The -p flag is used to create a parent directory if required. The ~ indicates the local user’s home directory and is equivalent to /home/user while the dot before the config file makes it hidden. Let’s create a simple bash script file that will write the memory usage to a text every 30 minutes. I am creating the script with the name of script.sh.

#! /bin/bash

while true

do

  free -m >> /home/user/myfile.txt

  sleep 1800

done

 

This script can be created anywhere, but ensure the path specified inside the script is an absolute path.

Now, let’s create a service that will execute the above script in the background. Launch any text editor, such as Nano or Vim, and paste the lines given below in it.

[Unit]

Description=My Service

[Service]

Type=simple

ExecStart=/bin/bash /home/user/script.sh

Restart=on-failure

[Install]

WantedBy=default.target

In the [Unit] section, the Description directive simply contains the name of the service. Note that it should not be more than 80 characters.

The [Service] section contains the three important directives. Firstly, the Type; which is simple, then ExecStart containing the executable of our custom script. The service will only restart when there is a failure.

The [Install] section contains the WantedBy directive which is default.target, implying that the service will be enabled at the system state when it is reached at the default run level, which is normally multi-user.target or graphical.target.

Now, save the file in the ~/.config/systemd/user directory with any name; I am naming it myservice.service.

How to Manage a Normal User Service

To manage the normal user service, the systemctl command is used with the –user flag. The –user flag signifies that the user is contacting the service manager, rather than the system.

After creating the user-specific service file, the first crucial step is to reload the systemd configuration files.

systemctl --user daemon-reload

This will apply the changes.

To know whether the service is running or not, use systemctl, with the –user flag and option.

systemctl --user status [service_name]

word-image-413453-3.png

Other commands to manage the normal user service are mentioned below:

systemctl --user start [service_name]

systemctl --user enable [service_name]

systemctl --user stop [service_name]

systemctl --user disable [service_name]

systemctl --user restart [service_name]

How to Create a Normal User Service with System Admin Permissions

There are many services that a user creates, but they require admin permission to run. Such services can be created by adding a User directive to the [Service] section.

The User directive can be used to mention the name of the user whose permissions are required to run the service, such as admin. So, if a normal user wants to create a service that demands admin permissions, then simply adding the User=admin in the [Service] section will do the job. However, this service will remain active as long as the admin is active. Note that this service cannot directly be controlled by the admin.

Conclusion

The normal user can also create a systemd service, but it has to be placed in ~/.config/systemd/user directory. This service runs as long as the user who created it is logged in. These services are also managed through systemctl command but with –user flag. Which tells the systemd that the service is being called by the user, not the system. In this guide, I created a custom normal user service and mentioned systemctl commands to manage it. Moreover, I also highlighted a method to create a service that requires administrative privileges.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...