Use systemd instead of Upstart on CentOS 7


If you've just migrated from CentOS 6 to CentOS 7 then you may be surprised on that fact that there is no /etc/init directory anymore, and now you don't know how to create Upstart script service.

Also you may noticed that there is /etc/init.d/README file containing the following:

You are looking for the traditional init scripts in /etc/rc.d/init.d,
and they are gone?

Here's an explanation on what's going on:


So, they changed the way how you create script services. Now they call it systemd services and there is a plenty of documentation out there. However it would be much easier to start with a simple example.

Let's create a simple PHP-based service that writes current time into the text file every second.

systemd services live at /etc/systemd/system directory. Let's create a file heartbeat.service there, and put a following content into it:

Description=Heartbeat service

ExecStart=/usr/bin/php /path/to/your/services/heartbeat.php

The config file is pretty self-documented. Update the path to your PHP file in the snippet above with a path where you want to keep things like that. As you can see, I configured the service so it could run as nginx user.

Now let's create a file /path/to/your/services/heartbeat.php and put there this PHP code:

    file_put_contents(__DIR__ . '/heartbeat.log', date(DATE_RFC2822) . PHP_EOL, FILE_APPEND);

Every time you create or update your .service config file you should reload the daemon, so let's do it now:

systemctl daemon-reload

And that's it, we're ready to launch our little service:

systemctl start heartbeat

Make sure that the service's user has a write access to the directory where heartbeat.php is located (or update the path in heartbeat.php). Go to this directory and run tail -f heartbeat.log. You should see the text like that:

Sun, 03 Nov 2019 20:20:59 +0000
Sun, 03 Nov 2019 20:21:00 +0000
Sun, 03 Nov 2019 20:21:01 +0000
Sun, 03 Nov 2019 20:21:02 +0000
Sun, 03 Nov 2019 20:21:03 +0000
Sun, 03 Nov 2019 20:21:04 +0000
Sun, 03 Nov 2019 20:21:05 +0000
Sun, 03 Nov 2019 20:21:06 +0000

Nice, it works! To stop the service just run:

systemctl stop heartbeat

Tons of boring documentation on systemd can be found here Follow the links that start with systemd.

Rate this post:
Share this page:

See also how to:

How to install PHP FastCGI Process Manager (FPM) on CentOS 7
How to install PostgreSQL database server on Linux
A very simple steps to install PHP 7 on CentOS 7
How to install Java runtime environment on CentOS 7
How to install MySQL database server on CentOS 7