15

I created a super basic init.d script for my python bot:

#!/bin/bash
# chkconfig: 2345 20 80
# description: Description comes here....

# Source function library.
. /etc/init.d/functions

start() {
    echo "starting torbot"
    python /home/ctote/dev/slackbots/torbot/torbot.py
    # example: daemon program_name &
}

stop() {
    # code to stop app comes here
    # example: killproc program_name
}

case "$1" in
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       # code to check status of app comes here
       # example: status program_name
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

And have set torbot.py to be +x and #!/usr/local/bin/python at the top. When I try to actually start it though, I get:

:/var/lock/subsys$ sudo service torbot start Failed to start torbot.service: Unit torbot.service not found.

Am I missing something?

ctote
  • 565
  • 1
  • 5
  • 13

5 Answers5

4

If you are using ubuntu 16.04 or newer you may want to review the doc of systemd about creating service files

The script is for the old init system and is managed by a legacy compatibility layer.

theist
  • 842
2

Ok, I tried some steps this stackoverflow answer(Running upstart script on 17.04?) and they worked My env is as follows

  1. Ubuntu at 17.10
  2. I have a python app on Gunicorn 19.x server, I need to start this application as a service.

Firstly you need to write a foo.service file.

[Unit] 
Description=FooServer 

[Service] 
Restart=on-failure
WorkingDirectory=/path/to/your/working/directory/where the foo lives
ExecStart=/what/process/will call foo eg: in my case I used gunicorn app:app
ExecReload=/bin/kill -HUP $MAINPID 
KillSignal=SIGINT 

[Install] 
WantedBy=multi-user.target

The meaning of every word on the left hand side of the '=' sign and their equivalent in (to the earlier) upstart is in link https://wiki.ubuntu.com/SystemdForUpstartUsers

Once the file is ready, let's say you name it as 'foo.service' (the .service extension is important)

You need to place the file in /lib/systemd/system

After which you need to enable the service by calling

systemctl enable foo

Which will prompt you enter your root password as it will be creating symlinks.

If you have reached till here without any hassle , you are good. Your service is hence created Start is by calling

sudo service foo start

systemctl status foo to see the status sudo service foo stop to stop the service

2

For me, I'm using Ubuntu 16.04.

Firstly change the init function

. /etc/init.d/functions

to

. /lib/lsb/init-functions

Then in shell, create symbolic links from /etc/rc* to my script:

sudo update-rc.d <myapp> defaults 95
0

I had the same issue, this is the solution that worked for me. Try:

sudo systemctl daemon-reload

sudo systemctl enable daemon_app.service

sudo systemctl start daemon_app.service

0

Have you tired something like this? How do I debug Upstart scripts?

Can you provide the output to that this guide provides so that we can help you potentially debug your issue?