0

I just started to learn about systemctl, I tried to create a service file that will run a nodejs script but it fails to run and the error doesn't really tell me why.

Here are the steps I've taken:

1) Navigated to /lib/systemd/system and created a file using the following command:

sudo nano /lib/systemd/system/hello_env.service

2) Created the following service file:

[Unit]
Description=
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/node /home/dev/index.js
Restart=always

[Install]
WantedBy=multi.user.target

3) typed sudo systemctl daemon-reload

4) Started the process:

sudo systemctl start hello_env

5) Checked status:

sudo systemctl status hello_env

And got the following error:

> hello_env.service   Loaded: loaded
> (/lib/systemd/system/hello_env.service; disabled; vendor pres   
> Active: failed (Result: exit-code) since Wed 2020-01-29 16:00:37 IST;
> 2s ago   Process: 25605 ExecStart=/usr/bin/node
> /home/dev/index.j  Main PID: 25605 (code=exited,
> status=1/FAILURE)
> 
> Jan 29 16:00:37 sf-env systemd[1]: hello_env.service: Main process
> exited, code Jan 29 16:00:37 sf-env systemd[1]: hello_env.service:
> Failed with result 'exit- Jan 29 16:00:37 sf-env systemd[1]:
> hello_env.service: Service hold-off time ove Jan 29 16:00:37 sf-env
> systemd[1]: hello_env.service: Scheduled restart job, re Jan 29
> 16:00:37 sf-env systemd[1]: Stopped index.js - starting the bot. Jan
> 29 16:00:37 sf-env systemd[1]: hello_env.service: Start request
> repeated to Jan 29 16:00:37 sf-env systemd[1]: hello_env.service:
> Failed with result 'exit- Jan 29 16:00:37 sf-env systemd[1]: Failed to
> start index.js.

Please note that If I navigate to /home/dev/index.js and run the script with node index.js it works just fine - please advise what I'm missing

5
  • Try to replace type=simple with type=forking.
    – Panki
    Commented Jan 29, 2020 at 14:53
  • doesn't work, getting error "Job for hello_env.service failed because the control process exited with error code"
    – Dante
    Commented Jan 29, 2020 at 14:59
  • Did you run systemctl daemon-reload afterwards? Does your script exit with a 0 if it works?
    – Panki
    Commented Jan 29, 2020 at 15:00
  • Yes I have, it didn't let me run systemctl start without it
    – Dante
    Commented Jan 29, 2020 at 15:01
  • As is so often the case, the log output that tells you what is going on is the part that comes before "Main process exited", that you haven't looked at or put into the question for other people to look at. And you've truncated the log lines that you did retrieve.
    – JdeBP
    Commented Feb 2, 2020 at 12:44

1 Answer 1

0

I managed to resolve it by echoing this line ExecStart=/usr/bin/node /home/dev/index.js, I then saw there was an error in the script.

So for future reference, debug the ExecStart to see that it actually works.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .