2

I have followed this tutorial to install uwsgi with nginx: Setting up Nginx and uWSGI for CGI scripting.

It all works, but it only works after I execute the command

sudo -u www-data ./uwsgi ./uwsgi_config.ini

I have to mention that I run headless and do all the work from a ssh terminal.

After I execute the command I cannot do anything else, unless I stop uwsgi with Ctrl-C (it just shows when a script a running). How to keep it running in the background and start it on startup? I tried to add it to .bashrc but I still cannot use the console until I end uwsgi.

  • Hello and welcome. I think you're missing something in that listed command. – Ghanima Mar 13 '16 at 21:36
  • I am able to run it in the background by adding -d file.log but I still can t start it with nginx. I need to start it up manually. – Cezar Mar 13 '16 at 22:02
1

You can use screen with a cronjob. This is what I do when I need to run something in the background at boot.

To do this, you will need to install screen with the command:

sudo apt-get install screen

Once that's done, edit your crontab file with:

crontab -e

and add this line to the bottom:

@reboot screen -d -m -S uwsgi sudo -u www-data ./uwsgi ./uwsgi_config.ini

and then reboot. It should be running in the background once your boot has finished. To attach the screen and see the script running, enter the command:

screen -r uwsgi

If you want to exit that screen without killing it, hit CTRL+A+D. (All three keys at the same time.)

  • I did it using just crontab. Thank you, screen was not needed, I just added -d file.log, created a file.log with permission 754 and that s it. – Cezar Mar 13 '16 at 22:03
  • @Cezar Happy to help! – Patrick Cook Mar 13 '16 at 22:04
  • Upvote because this will work, but the best way would be to just use systemd! – Will Mar 14 '16 at 0:10
2

The "right way" to do this is to create a systemd service.

Put the following in /etc/systemd/system/uwsgi.service (make sure to change /path/to/project/directory):

[Unit]
Description=uWSGI instance

[Service]
ExecStartPre=-/bin/bash -c 'mkdir -p /run/uwsgi; chown www-data:www-data /run/uwsgi;'
ExecStart=/bin/bash -c 'cd /path/to/project/directory; uwsgi --ini uwsgi_config.ini'

[Install]
WantedBy=multi-user.target

Then run:

systemctl daemon-reload
systemctl enable uwsgi

Then restart the system; it will start on boot now!

While screen may work; it's a hack. The init system is how programs are started and stopped automatically under Linux, and systemd is the modern init system used by Raspbian.

Anything you put in ExecStartPre will be run before the program is started, and ExecStart is the command used to actually run the program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.