[LINUX] When I start service with systemd, I get command not found even though the path is passing

The problem you are having

Premise

I'm trying to set up a Django server with Nginx + uWSGI on Ubuntu 18.04 on an Amazon EC2 instance.

** Various versions ** Nginx: 1.14.0 uWSGI: 2.0.18 Python: 3.7.5 Django: 2.2.11

First, in order to start uWSGI, I created the following file called uwsgi-hogehoge.service.

[Unit]
Description=uWSGI service for mysite

[Service]
User=ubuntu
ExecStart=/bin/bash -c 'sh /hogehoge/start.sh'
ExecStartPre=/bin/bash -c 'sh /hogehoge/start_pre.sh'
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

start.sh contains the process to start uwsgi.

And what happened

I created a service file to start uWSGI and Nginx, and put a symbolic link to systemd. And

$ systemctl start uwsgi-hogehoge.service

When I try to start the service with, apparently it is not possible as far as I can see the status.

$ journalctl -xe

When I checked the log with, the following error was displayed.

uwsgi: command not found

What I confirmed

・ The uwsgi pass is normal. -It is not an error related to permissions such as permissions. -Even if there is an error around the authority in the first place, the appropriate authority is given to the file and the user.

From the above, it seems that uwsgi's path is normally passed, but I questioned the hypothesis that systemd's path is not passed only when it is executed as a startup command.

solution

After all there is a great ancestor in everything. I found the following god article and solved everything.

Make systemd read user environment variables

Quoting from the article above,

When I looked it up, systemd was in .barshrc and .bash_profile I found that it didn't read the environment variables I defined.

It seems that ...

What I did to solve

$ echo $PATH

Display the path with. Then

/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.local/bin

It was displayed like this. Then I created a sysconfig directory in / etc and created a file with an appropriate name. In my case, I was a user named ubuntu, so I changed the file name to ubuntu. And I edited the ubuntu file as follows.

# cat <_EOF_ > /etc/sysconfig/ubuntu
PATH=/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.local/bin/usr/local/bin
_EOF_

Next, set the path to the file you created earlier in the uwsgi-hogehoge.service file.

[Unit]
Description=uWSGI service for mysite

[Service]
User=ubuntu
EnvironmentFile=/etc/sysconfig/ubuntu <--- Add here! !!
ExecStart=/bin/bash -c 'sh /hogehoge/start.sh'
ExecStartPre=/bin/bash -c 'sh /hogehoge/start_pre.sh'
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Now you can read environment variables properly when you start the service with systemd.

$ systemctl daemon-reload

Load the service file update properly and try again

$ systemctl start uwsgi-hogehoge

This time it started properly.

Recommended Posts

When I start service with systemd, I get command not found even though the path is passing
Investigate the cause when an error is thrown when python3.8 is not found when using lambda-uploader with python3.8
[Super Beginner] [Tired Intermediate] When "command not found" or "command not found" is displayed
A story when a directory is buggy with the django-admin start project command in a virtual environment using Pipenv
Command is not found in sudo
If you try to start the jupyter notebook installed with pip and it says "command not found", it seems that you can go through the PATH.
I get a Python No module named'encodings' error with the aws command
When the Linux command $ yum -y install gives an error (yum: command not found)
VS Code freezes & PC crashes when I start the server with go
I get an error ~ is zero, singular U when passing the covariance matrix from the Linear layer to MultivariateNormal
When I try to connect django and postgresql with Docker, I get the error "django.db.utils.OperationalError: could not translate host name" db "to address: Name or service not known"
When I get an error with PyInstaller
Get the path to the systemd unit file
I want to get the path of the directory where the running file is stored.
When the log acquired by rsyslog is not updated after rotation with logrotate
I want to be notified when the command operation is completed on linux!