DEV Community

Cover image for Run background jobs (eg. queue listener) on an environment that does not allow root access.
Dimitrios Desyllas
Dimitrios Desyllas

Posted on • Edited on

Run background jobs (eg. queue listener) on an environment that does not allow root access.

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/
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Usually upon authoring service file upon Install section contains:

WantedBy=multi.target
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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  

Enter fullscreen mode Exit fullscreen mode

Then launch the service. In our case we run:

systemctl --user start queue_runner.service
Enter fullscreen mode Exit fullscreen mode

Then ensure it run upon rebbot via:

systemctl --user enable queue_runner.service
Enter fullscreen mode Exit fullscreen mode

We can use these commands for logs:

systemctl --user status queue_runner.service
journalctl --user -u queue_runner.service -f
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

And run:

systemctl --user daemon-reload
systemctl --user start queue_runner.service
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Saves the output of stdout in the file, whilst the:

StandardError
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

(Replace /home/myapp/public_html/var/log with your own path, then update service file accordingly.)

Top comments (0)