{"id":2302,"date":"2013-08-27T09:31:44","date_gmt":"2013-08-27T09:31:44","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=2302"},"modified":"2021-12-22T09:10:27","modified_gmt":"2021-12-22T08:10:27","slug":"process-controlling-with-supervisor","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/process-controlling-with-supervisor\/","title":{"rendered":"Process Controlling With Supervisor"},"content":{"rendered":"<p>If you run your own virtual or dedicated server, chances are that you want to run a specific program or a number of programs that make up the service you want to use or offer. If the program you want to run is part of the Linux distribution of your choice there usually is not much more to it than installing the package and configuring the program. However, if the program comes from an external source or you are writing it yourself, you need to make sure it is started automatically when the server is booting. Additionally, during development or early testing phases of your own program, there might be errors in the code leading to a crash of your application and you might want to make sure that it gets restarted automatically in such a case. A few years ago, the solution to the first issue was quite simple. All you needed to do was to create an init script that would then handle the starting and stopping of the server. However, recently many Linux distributions changed the way they handle the boot process. Some are still using <code>init<\/code>, others may be using <code>upstart<\/code> or even <code>systemd<\/code> now. Providing the files necessary for all of this systems can be quite a hassle and while <code>upstart<\/code> and <code>systemd<\/code> support restarting programs on unexpected termination, implementing this with <code>init<\/code> is possible but requires to change <code>init<\/code>&#8216;s configuration itself.<\/p>\n<p>For my own needs I have become attached to using <strong>supervisor<\/strong> for this task &#8211; the program and the documentation can be found on <a href=\"http:\/\/supervisord.org\/\" rel=\"nofollow\">http:\/\/supervisord.org<\/a>, but most Linux distributions provide pre-built packages in their repositories. Supervisor itself is a daemon that is run by the system&#8217;s process management so it gets run by <code>init<\/code> or it&#8217;s counterparts. To run your own program, you have to add it to supervisord&#8217;s configuration and it will make sure that it gets started on boot and restarted in case it crashes.<\/p>\n<p>If you\u2019re not sure about your admin rights, always enter the command sudo -i at the beginning of every session:<\/p>\n<p><code>sudo -i<\/code><\/p>\n<p>This command will grant you the rights of a power user, so you don\u2019t have to write the command sudo at the beginning of every command line.<\/p>\n<p>As an example, I will be using a very small custom web application written in Python using the bottle framework. Since this article is not about web programming, I am keeping it simple:<\/p>\n<p><code>1 from bottle import route, run<br \/>\n2<br \/>\n3 @route('\/')<br \/>\n4 def index():<br \/>\n5&nbsp;&nbsp;&nbsp;&nbsp; return 'Hello World'<br \/>\n6<br \/>\n7 run(host='0.0.0.0', port=8080)<\/code><\/p>\n<p>All this does is run a webserver on port 8080 and displaying <code>Hello World <\/code> in your web browser when you navigate to it. If the above code is saved to a file <code>app.py<\/code>, you can run it using <code>python app.py<\/code> and it will just run forever (or until it crashes). Now would be a good time to configure supervisor to run this application for us. Supervisor provides a command line tool called <code>supervisorctl<\/code> to check the status of configured applications and to start or stop them if needed. Running <code>supervisorctl status<\/code> will show you&#8230; nothing, as we did not set up anything yet. We create a new file called <code>hello.conf<\/code> which will contain everything supervisor needs to know to run our application and place it in <code>\/etc\/supervisor\/conf.d\/.<\/code> The most basic configuration defines a new program to run with a given name and a command to be executed as well as a user name that the program should be run with &#8211; if you leave this, your program will run as root which is almost always a bad idea:<\/p>\n<p><code>[program:hello]<br \/>\ncommand=\/usr\/bin\/python \/home\/markus\/app.py<br \/>\nuser=markus<\/code><\/p>\n<p>Note that it is usually a good idea to provide absolute paths in such configurations. After the file has been saved, you can use <code>supervisorctl reread<\/code> to cause supervisor to reread its configuration file. If everything is right, the output of the command should tell you that a program named <code>hello<\/code> is now available:<\/p>\n<p><code># supervisorctl reread<br \/>\nhello: available<\/code><\/p>\n<p>We can now start the program by running<\/p>\n<p><code># supervisorctl start hello<br \/>\nhello: started<\/code><\/p>\n<p>Check the status again to see if its actually running now:<\/p>\n<p><code># supervisorctl status<br \/>\nhello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RUNNING&nbsp;&nbsp;&nbsp; pid 32675, uptime 0:00:46<br \/>\n<\/code><\/p>\n<p>As you can see, it tells us that the program is running, it&#8217;s PID and the time elapsed since it&#8217;s start. To simulate a crash, we will forcefully terminate the program and check if supervisor restarts it as expected:<\/p>\n<p><code># kill -9 32675<br \/>\n# supervisorctl status<br \/>\nhello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RUNNING&nbsp;&nbsp;&nbsp; pid 32686, uptime 0:00:04<br \/>\n<\/code><br \/>\nThe supervisor homepage gives you a lot more information about possible values to configure supervisor itself and the programs it runs. You can even configure the location of log files and automatic rotation for them in case they grow over a given size. More details about this can be found here: <a href=\"http:\/\/supervisord.org\/configuration.html\" rel=\"nofollow\">http:\/\/supervisord.org\/configuration.html<\/a>. Finally, no more reason to run your applications in detached screen sessions&#8230; \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you run your own virtual or dedicated server, chances are that you want to run a specific program or a number of programs that make up the service you want to use or offer. If the program you want to run is part of the Linux distribution of your choice there usually is not [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[176,254,804,845,881,886,910],"ppma_author":[1503],"class_list":["post-2302","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-configuration","tag-dedicated-server","tag-supervisor","tag-tutorial","tag-virtual-server","tag-virtuelle-server","tag-web-application"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Markus","author_link":"https:\/\/contabo.com\/blog\/author\/markush\/"},"uagb_comment_info":0,"uagb_excerpt":"If you run your own virtual or dedicated server, chances are that you want to run a specific program or a number of programs that make up the service you want to use or offer. If the program you want to run is part of the Linux distribution of your choice there usually is not&hellip;","authors":[{"term_id":1503,"user_id":20,"is_guest":0,"slug":"markush","display_name":"Markus","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/ec941657f54e47d08edadc14f82d2e9f72f02cc924e22920292a2889eaa89a41?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/2302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=2302"}],"version-history":[{"count":0,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/2302\/revisions"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=2302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=2302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=2302"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=2302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}