4

I am trying to make a custom service in my VPS. However, it doesn't let me to run it. It says the following. I am not sure what is going on but when I run it on my Ubuntu laptop, it works properly. What could be the problem?

sudo systemctl start websocket.service

Failed to issue method call: Unit websocket.service failed to load: No such file or directory. See system logs and 'systemctl status websocket.service' for details.

cat /lib/systemd/system/websocket.service

[Unit]
Description=php webSocket
After=syslog.target network.target

[Service]
User=root

Type=simple
ExecStart=/usr/bin/webs.sh
TimeoutStopSec=20
KillMode=process
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target
Alias=websocket.service

cat /usr/bin/webs.sh

#!/bin/bash
### BEGIN INIT INFO
# Provides:          webSocket
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: php webSocket
# Description:       php webSocket
### END INIT INFO
/usr/bin/php /path/to/server.php

I have tried systemctl daemon-reload but I get the following

Attempted to remove disk file system, and we can't allow that.
Ignoring /etc/systemd/system/multi-user.target.wants/ssh.service -> /lib/systemd/system/ssh.service for systemd deputy init
Ignoring /etc/systemd/system/multi-user.target.wants/rsyslog.service -> /lib/systemd/system/rsyslog.service for systemd deputy init
Ignoring /etc/systemd/system/multi-user.target.wants/bind9.service -> /lib/systemd/system/bind9.service for systemd deputy init
Ignoring /etc/systemd/system/timers.target.wants/phpsessionclean.timer -> /lib/systemd/system/phpsessionclean.timer for systemd deputy init

Can anyone give me some hint of what is going on?

This are the permissions for the service file:

-rwxr-xr-x 1 root root  264 Feb  6 05:06 websocket.service*

And this for the bash file:

-rwxr-xr-x 1 root root 349 Feb  6 05:02 webs.sh*

I changed the webs.sh file to /usr/bin/ path and updated the file as @TeroKilkanen mentioned but i still having same problem.

kobbycoder
  • 151
  • 1
  • 9

1 Answers1

0

One issue here is that you are adding the script to the root directory, which is not the place for scripts. /usr/local/bin is a better place for scripts like this.

Second issue is that your shebang is incorrect. Shebang only includes the path to the executable that will be used to execute the script, not any arguments to it. This is what causes the No such file or directory. error. https://unix.stackexchange.com/questions/63979/shebang-line-with-usr-bin-env-command-argument-fails-on-linux tells more about this behaviour.

You can use this approach instead:

For webs.sh use this:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          webSocket
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: php webSocket
# Description:       php webSocket
### END INIT INFO
/usr/bin/php /path/to/server.php

Remember to give executable permissions to the file.

I wouldn't be worried about Michael's comment just yet, the rest of systemd output could be a symptom of this wrong setup.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • 1
    The shell script could be omitted entirely, if that's all that needs to be done. – Michael Hampton Feb 06 '17 at 07:50
  • i modified everything as you mentioned but still having same error, the webs.sh is working properly but not the service file, the managers told me this, "- Make sure the .service file is set as executable - Make sure the .service file has the correct permissions" how do i know if the service file is executable? what i did was, chmod +x websocket.service but still saying same error – kobbycoder Feb 06 '17 at 10:16