I'm using Ubuntu 16.04 Server.

I want to run specific command everytime when the server starts. I mean that this command should be executed when the server runs (continual work).

This command is connected to PHP framework and starts web application. Normally I can run this command going to the folder where the app is located /var/www/html/app and then I execute php artisan serve --host ipadress

How to make it works?

Thanks in advance!

  • You have many choices: systemd, upstart or systemctl. – pylover Aug 31 '16 at 9:14
  • Ok, but could you give me some example how to run this command firstly entering to the directory and then run this command? – Martin Aug 31 '16 at 9:19
  • see the chdir, exec and setuid/setgid in my answer. – pylover Aug 31 '16 at 9:40

Some programs are not designed to be run with continuous user input and disconnect from the terminal at the first opportunity. For example, a web server responds to web requests, rather than user input. Mail servers are another example of this type of application. These types of programs are known as daemons. The term daemon comes from Greek mythology and represents an entity that is neither good nor evil, and which invisibly performs useful tasks. This is why the BSD mascot is the cheerful-looking daemon with sneakers and a pitchfork.

Let using upstart:

Create a file named: /etc/init/my-http-server.conf

description "my-http-server"

start on (filesystem)
stop on runlevel [016]

#setuid my_user_id
#setgid my_group_id
#chdir /opt/my/env

exec <your command>

pre-start script
  mkdir -p /var/run/my/
  chown -R my:www-data /var/run/my  
end script

#post-start script
#  sleep 6
#  chmod 776 /var/run/my/.s.my
#end script

For more information refer to : upstart documents.

Then use start/stop/restart commands to control your daemon:

$ start my-http-server
$ stop my-http-server
$ restart my-http-server

For a complete list of available choices to make a daemon process on ubuntu 16.04: see here

EDIT 1: I have some additional info: The ubuntu 16.04 comes with systemd.

So, the best practice is to make a systemd daemon. because the rc.d, init.d and upstart will be deprecated very soon!.

| improve this answer | |
  • I did it, but unfortunetly when I start my-http-server I'm getting this: start: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/ups – Martin Aug 31 '16 at 9:43
  • its up to your configurations. check permissions and paths in ypur application config. and do not use: pre-start script and post-start script. it's just for example. – pylover Aug 31 '16 at 9:47
  • It's highly recommended to read up the upstart documentation carefully. and: DO NOT DO ANYTHING IF YOU DON'T KNOW WHAT YOU'RE DOING – pylover Aug 31 '16 at 9:49
  • Now I don't know what to do. I should remove the "pre-start script" totally and then what? – Martin Aug 31 '16 at 9:54
  • Then read the upstart documentation. – pylover Aug 31 '16 at 11:18

You can create a cron entry using @reboot for that purpose. It will be executed once at startup.

You can either put a file into /etc/cron.d with this content:

 @reboot    root    cd /var/www/html/app && /usr/bin/php artisan serve --host ipaddress

You should change root to a different user if the command doesn't need root privileges.

Alternatively you can run crontab -e -u username to edit the crontab of the user which should execute the command and insert this line into it:

 @reboot    cd /var/www/html/app && /usr/bin/php artisan serve --host ipaddress

Because of the && the command is only executed if the cd was successful.

Update: If it is a server process, you are better off using an init system like pylover indicated. I would prefer systemd on ubuntu because they just changed to it and i'm not sure how long the others (upstart) will be available without having to install or configure anything. An init system gives you the benefit of being able to start and stop the service and query its status.

| improve this answer | |
  • Okay, it looks nice, but where to put this code? I am newbie in linux. – Martin Aug 31 '16 at 9:28
  • 1
    This is the cheapest way to do that, a regular and perfect solution is daemons. – pylover Aug 31 '16 at 9:39
  • 1
    You're right. I read it as if it was a one time job. Servers shouldn't be started using cron. – Hoov Aug 31 '16 at 9:44
  • I've created file in /etc/cron.d/laravel and paste this command, nothing happens. – Martin Aug 31 '16 at 10:06

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.