Steps to Create a Linux executable as a systemd service
- Login to Linux server via Putty. Ensure the login id you use has access to create systemd services.
- Navigate to /etc/systemd/system folder by executing command "cd /etc/systemd/system"
- Open a new file in vi editor to create as a service file. You can name the file appropriately with a .service extension
- Enter I for insert mode, Copy following content to the file, change description, working directory and ExecStart to match your service description, service executable directory and service executable file path respectively. Value for user can be changed if there is any user other than root who has access to create and manage the service.[Unit]
Description=tetherfi tunnelclient service
After=network.target[Service]
Type=simple
Restart=always
RestartSec=5
User=root
WorkingDirectory=/home/qa/SMM/TunnelClient
ExecStart=/home/qa/SMM/TunnelClient/callbackserveroutboundcommagent-linux[Install]
WantedBy=multi-user.target - Hit Esc key, type :wq to save file. To verify file contents were saved, execute command "cat <filename>" and the content as expected should be shown
- Now execute command "systemctl enable <service file name>". This will enable service to start automatically whenever server reboots
- To start the service, execute command "systemctl start <service file name>"
- To Check status of the service, execute command "systemctl status <service file name>". Service will show status as Active (running) indicating start was successfull.
- If you need to stop the service anytime, execute command "systemctl stop <service file name>"
- Please note that this procedure will execute a linux executable as a service only when it needs to be executed without any parameters. If the start command is to have parameters, the ExecStart value in service file needs to reflect that.