7 min read

In this tutorial, we’ll learn the basics of setting up Odoo as a system service. 

This article is taken from the book Odoo 12 Development Essentials by Daniel Reis. This book will help you extend your skills with Odoo 12 to build resourceful and open source business applications.

Setting up and maintaining servers is a non-trivial topic in itself and should be done by specialists. The information given here is not enough to ensure an average user can create a resilient and secure environment that hosts sensitive data and services.

In this article, we’ll discuss the following topics:

  • Setting up Odoo as a system service, including the following:
    • Creating a systemd service
    • Creating an Upstart or sysvinit service
    • Checking the Odoo service from the command line

The code and scripts used here can be found in the ch14/ directory of the Git repository.

Setting up Odoo as a system service

We will learn how to set up Odoo as a system service and have it started automatically when the system boots.

In Ubuntu or Debian, the init system is responsible for starting services. Historically, Debian (and derived operating systems) has used sysvinit, and Ubuntu has used a compatible system called Upstart. Recently, however, this has changed, and the init system used in both the latest Debian and Ubuntu editions is systemd.

This means that there are now two different ways to install a system service, and you need to pick the correct one depending on the version of your operating system.

On Ubuntu 16.04 and later, you should be using systemd. However, older versions are still used in many cloud providers, so there is a good chance that you might need to use it.

To check whether systemd is used in your system, try the following command:

$ man init

This command opens the documentation for the currently init system in use, so you’re able to check what is being used.

Ubuntu on Windows Subsystem for Linux (WSL) is an environment good enough for development only, but may have some quirks and is entirely inappropriate for running production servers. At the time of writing, our tests revealed that while man init identifies the init system as systemd, installing a systemd service doesn’t work, while installing a sysvinit service does.

Creating a systemd service

If the operating system you’re using is recent, such as Debian 8 or Ubuntu 16.04, you should be using systemd for the init system.

To add a new service to the system, simply create a file describing it. Create a /lib/systemd/system/odoo.service file with the following content:

[Unit] 
Description=Odoo 
After=postgresql.service 
 
[Service] 
Type=simple 
User=odoo 
Group=odoo 
ExecStart=/home/odoo/odoo-12/odoo-bin -c /etc/odoo/odoo.conf 
 
[Install] 
WantedBy=multi-user.target

The Odoo source code includes a sample odoo.service file inside the debian/ directory. Instead of creating a new file, you can copy it and then make the required changes. At the very least, the ExecStart option should be changed according to your setup.

Next, we need to register the new service with the following code:

$ sudo systemctl enable odoo.service

To start this new service, use the following command:

$ sudo systemctl start odoo

To check its status, run the following command:

$ sudo systemctl status odoo

Finally, if you want to stop it, use the following command:

$ sudo systemctl stop odoo

Creating an Upstart or sysvinit service

If you’re using an older operating system, such as Debian 7 or Ubuntu 15.04, chances are your system is sysvinit or Upstart. For the purpose of creating a system service, both should behave in the same way. Some cloud Virtual Private Server (VPS) services are still based on older Ubuntu images, so this might be aware of this scenario in case you encounter it when deploying your Odoo server.

The Odoo source code includes an init script used for the Debian packaged distribution. We can use it as our service init script with minor modifications, as follows:

$ sudo cp /home/odoo/odoo-12/debian/init /etc/init.d/odoo
$ sudo chmod +x /etc/init.d/odoo

At this point, you might want to check the content of the init script. The key parameters are assigned to variables at the top of the file, as illustrated in the following example:

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin 
DAEMON=/usr/bin/odoo 
NAME=odoo 
DESC=odoo 
CONFIG=/etc/odoo/odoo.conf 
LOGFILE=/var/log/odoo/odoo-server.log 
PIDFILE=/var/run/${NAME}.pid 
USER=odoo

These variables should be adequate, so we’ll prepare the rest of the setup with their default values in mind. However, you can, of course, change them to better suit your needs.

The USER variable is the system user under which the server will run. We have already created the expected odoo user.

The DAEMON variable is the path to the server executable. Our executable used to start Odoo is in a different location, but we can create the following symbolic link to it:

$ sudo ln -s /home/odoo/odoo-12/odoo-bin /usr/bin/odoo
$ sudo chown -h odoo /usr/bin/odoo

The CONFIG variable is the configuration file we need to use. In a previous section, we created a configuration file in the default expected location, /etc/odoo/odoo.conf.

Finally, the LOGFILE variable is the directory where log files should be stored. The expected directory is /var/log/odoo, which we created when we defined the configuration file.

Now we should be able to start and stop our Odoo service, as follows:

$ sudo /etc/init.d/odoo start
Starting odoo: ok

Stopping the service is done in a similar way with the following command:

$ sudo /etc/init.d/odoo stop
Stopping odoo: ok

In Ubuntu, the service command can also be used, as follows:

$ sudo service odoo start
$ sudo service odoo status
$ sudo service odoo stop

Now we need to make the service start automatically on system boot; this can be done with the following code:

$ sudo update-rc.d odoo defaults

After this, when we reboot our server, the Odoo service should start automatically and with no errors. It’s a good time to verify that all is working as expected.

Checking the Odoo service from the command line

At this point, we can confirm whether our Odoo instance is up and responding to requests as expected.

If Odoo is running properly, we should be able to get a response from it and see no errors in the log file. We can check whether Odoo is responding to HTTP requests inside the server by using the following command:

$ curl http://localhost:8069
<html><head><script>window.location = '/web' +  location.hash;</script></head></html>

In addition, to see what is in the log file, use the following command:

$ sudo less /var/log/odoo/odoo-server.log

You can also follow what is being added to the log file live, using  tail -f as follows:

$ sudo tail -f /var/log/odoo/odoo-server.log

Summary

In this tutorial, we learned about the steps required for setting up Odoo as a system service.

To learn more about Odoo, you should read our book  Odoo 12 Development Essentials. You may also take a look at the official documentation at https://www.odoo.com/documentation.

Odoo is an open source product with a vibrant community. Getting involved, asking questions, and contributing is a great way not only to learn but also to build a business network. With this in mind, we can’t help mention the Odoo Community Association (OCA), which promotes collaboration and quality open source code. You can learn more about it at odoo‑comunity.org.

Read Next

“Everybody can benefit from adopting Odoo, whether you’re a small start-up or a giant tech company – An interview by Yenthe van Ginneken.

Implement an effective CRM system in Odoo 11 [Tutorial]

Handle Odoo application data with ORM API [Tutorial]

Content Marketing Editor at Packt Hub. I blog about new and upcoming tech trends ranging from Data science, Web development, Programming, Cloud & Networking, IoT, Security and Game development.