I have created a service which does some database connection and does some query. I want to know if I want to enable "service xyzservice start" or "service xyzservice stop" or restart where Do i need to make this service entry in Ubuntu? what are the steps.


When you say "I have created a service", I wonder if you mean "I have created a shell script" or something similar. As creating a service would involve writing a .service file, which is 90% of the challenge in performing the task.

So, for the sake of this answer, I'm going to assume that you've created a shell script called query_db.sh. The next step will be to create the .service file, then enable it.

Here is how you create a service:

  1. Open Terminal (if it's not already open)
  2. Create a new file for the service. I will call mine query_db.service:
    sudo vi /etc/systemd/system/query_db.service
    Note: Feel free to use any text editor you wish. The use of vi in this example is neither a suggestion nor an endorsement. It's just a force of habit.
  3. Paste the following into the new file, editing the relevant values where appropriate:
    Description=Do Some Query
    Note: Be sure to change the User, WorkingDirectory, and ExecStart values to something correct. The other items can remain untouched if you are unsure of how to configure them.
  4. Verify that the script file is executable:
    sudo chmod +x /home/dheeraj/scripts/query_db.sh
  5. Reload the systemd daemons:
    sudo systemctl daemon-reload
  6. Enable your daemon:
    sudo systemctl enable query_db
  7. Start your daemon:
    sudo systemctl start query_db

That's all there is to it. You can now check the status of your service with:

sudo systemctl status query_db

And, of course, you can stop your service with:

sudo systemctl stop query_db

If you prefer to use the shorter commands, swap the action with the service name:

sudo service query_db status
sudo service query_db stop

That's really all there is to it.

  • On my ssystem getting error :bash: systemctl: command not found
    – DheerajK
    Jul 8 at 11:15
  • # cat /etc/os-release NAME="Ubuntu" VERSION="16.04.2 LTS (Xenial Xerus)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 16.04.2 LTS" VERSION_ID="16.04" HOME_URL="ubuntu.com" SUPPORT_URL="help.ubuntu.com" BUG_REPORT_URL="bugs.launchpad.net/ubuntu" VERSION_CODENAME=xenial UBUNTU_CODENAME=xenial I read on some sites we need to use Upstart if systemctl is not found , Can you help me with steps for making service able to restart/stop/start using Upstart?
    – DheerajK
    Jul 8 at 11:27
  • Nope. Apologies, but I cannot help. 16.04 has been EOL for a few months already and I don’t have any VMs of that version to do testing with anymore.
    – matigo
    Jul 8 at 11:41
  • Thanks Matigo , If you have any reference which I can use and try those things that would be helpful. I am done with code for 3 services but not able make them enable wrt this start/stop/restart operations.
    – DheerajK
    Jul 8 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.