Recently, in my work, I came across a peculiar problem. I needed to run reliably a queue listener. My go-to solution in this problem is to set up supervisor, but in order to do that requires root/sudo access and I did not have this privilege.
The server does ship with a Linux distro but running apt-get, or any other package manager, does require sudo
or su
in order to get root access. In my case I was unable to do that.
Well, supervisor is not the only option for utilizing a process manager. There's another option that also comes preinstalled on every linux distro, it is called systemd and many services use that.
Each user can run its own systemd services as non root-user.
First and foremost, you need to create the ~/.config/systemd/user/
mkdir -p ~/.config/systemd/user/
Then we need to place a file upon ~/.config/systemd/user/
, for example in my case it was a symfony queueu listener, therefore I run:
nano ~/.config/systemd/user/queue_runner.service
And placed the following contents
[Unit]
Description=Symfony Queue message Listener
StartLimitIntervalSec=20s
StartLimitBurst=5
[Service]
Type=simple
WorkingDirectory=/home/qpassstage/public_html_test
ExecStart=/usr/bin/php -d memory_limit=-1 bin/console messenger:consume async -vv
Restart=always
RestartSec=1
TimeoutSec=300
[Install]
WantedBy=default.target
It is important the filename to end with .service
and exist upon ~/.config/systemd/user/
instead of /etc/systemd/system/
where system-wide services live upon.
Also, it is important upon [Install]
section of the file to contain:
WantedBy=default.target
Usually upon authoring service file upon Install
section contains:
WantedBy=multi.target
The directive default.target
allows you to run the service as the default user instead of root user.
Furthermore upon the service file above the command we need to launch as a service is:
/usr/bin/php -d memory_limit=-1 bin/console messenger:consume async -vv
And is defined upon ExecStart
whereas upon WorkingDirectory
we define the location where command that will run as a service is located upon.
The section:
Restart=always
Ensures that once command unexpecteddly stops working then systemd would execute it once more.
Launcing Service
Once placed, you need to ensure that systemd has recognized the service:
systemctl --user daemon-reload
Then launch the service. In our case we run:
systemctl --user start queue_runner.service
Then ensure it run upon rebbot via:
systemctl --user enable queue_runner.service
We can use these commands for logs:
systemctl --user status queue_runner.service
journalctl --user -u queue_runner.service -f
In case your user is not upon journal
group use file instead by placing these directives upon service file:
[Unit]
Description=Symfony Queue message Listener
StartLimitIntervalSec=20s
StartLimitBurst=5
[Service]
Type=simple
WorkingDirectory=/home/qpassstage/public_html_test
ExecStart=/usr/bin/php -d memory_limit=-1 bin/console messenger:consume async -vv
Restart=always
RestartSec=1
TimeoutSec=300
StandardOutput=append:/home/myapp/public_html/var/log/messenger.log
StandardError=append:/home/myapp/public_html/var/log/messenger.log
[Install]
WantedBy=default.target
And run:
systemctl --user daemon-reload
systemctl --user start queue_runner.service
As you notice, we used the file /home/qpassstage/public_html/var/log/messenger.log
to store the output of the command the directive:
StandardOutput
Saves the output of stdout in the file, whilst the:
StandardError
Saves the output of stderr upon the file. In my case the directory /home/myapp/public_html/var/log
already existed, therefore I did not make it.
If the folder does not exist, using mkdir -p
you can make it:
mkdir -p /home/myapp/public_html/var/log
(Replace /home/myapp/public_html/var/log
with your own path, then update service file accordingly.)
Top comments (0)