Score:0

how to enable start/stop/restart for a newly added service in Ubuntu

ar flag

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.

Score:0
in flag

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:
    [Unit]
    Description=Do Some Query
    
    [Service]
    User=dheeraj
    WorkingDirectory=/home/dheeraj/scripts
    ExecStart=/home/dheeraj/scripts/query_db.sh
    Type=simple
    TimeoutStopSec=10
    Restart=on-failure
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target
    
    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.

DheerajK avatar
ar flag
On my ssystem getting error :bash: systemctl: command not found
DheerajK avatar
ar flag
# 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="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://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?
in flag
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.
DheerajK avatar
ar flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.