📜 ⬆️ ⬇️

Run the program when you start Mac OS X - Launchd

launchd
Let's start with the story. There were a dozen programs to run processes or programs. And thousands of scripts for them. Apple's Dave Zarzycki decided to write a program that would differ from the ones written above and offer a single, standardized interface to any programs that automatically start the system and other goodies.

That's the whole story. Now to the program itself.

It is called Launchd. And it is open source. Open Source
All processes at system startup (in Mac OS X) are launched by a single process launchd. When loaded, launchd is called by the kernel as the first process and then the whole system starts using it. Launchd also ensures that the process is running. If he suddenly falls, Launchd will help him and lift him.
')
First of all, let's decide why we need to run something at boot and when it should be done.
If you need a program or process to start at system startup and even when the user is not logged in, you should create a file in the / Library / LaunchDaemons folder. If it is needed only when you are logged in, then in the / Library / LaunchAgents folder. You can also use other directories, but in this article we will not touch them.

Let's create Daemon to run php-fcgi processes.
sudo mate /Library/LaunchDaemons/com.php-fcgi
And so what should we write there?
First, we clarify that the Property List is an Xml file. And it must be valid. Otherwise, nothing will work.
Textmate has a check on the validity of these files, so no spaces should arise.

This file must contain 3 mandatory keys.
Label is a unique identifier.
 <key>Label</key><string>php</string> 

ProgramArguments - In essence, this is the path and parameters of the program being launched.
The first line is always the program path if the Program key is not used.
Subsequent lines will be parameters. Each parameter and its value is better to write in different lines.
 <key>ProgramArguments</key> <array> <string>/opt/local/bin/php-cgi</string> <string>-b 127.0.0.1:1026</string> <string>-q</string> </array> 

OnDemand - This key tells Launchd to restart the process if it did not start or fell. Just setting the value to false indicates that we want the process to be always running.
 <key>OnDemand</key> <false/> 
<key>OnDemand</key> <false/>

Now we wrap it all into a standard entry present in all files of this type.
 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>php</string> <key>ProgramArguments</key> <array> <string>/opt/local/bin/php-cgi</string> <string>-b 127.0.0.1:1026</string> <string>-q</string> </array> <key>OnDemand</key> <false/> </dict> </plist> 

Let's save. Now Launchctl is a program interface for Launchd.
With it, we can enable or disable the download of our configuration file.
For example, the following command will include our file.
sudo launchctl load -w /Library/LaunchDaemons/com.php-fcgi.plist
And this one will turn it off.
sudo launchctl unload -w /Library/LaunchDaemons/com.php-fcgi.plist
And add the key with the parameter to our file.
  <key>Disabled</key><true/> 

In general, now we have to start 5 php-cgi processes at boot time. You can restart them with the command
sudo launchctl stop php
and since we have OnDemand false, it will simply restart the process.

Yes, I almost forgot. If you want to add environment variables, you need to write something like this
 <key>EnvironmentVariables</key> <dict> <key>PHP_FCGI_CHILDREN</key> <string>10</string> <key>PHP_FCGI_MAX_REQUESTS</key> <string>1000</string> </dict> 

And now the bonuses
For many, digging into the command line is lazy or difficult. For them, there is an easier option. This is a free Lingon program.
Lingon

Also with the help of this program you can open com.apple.syslogd
and instead of / usr / sbin / syslogd write / usr / sbin / syslogd -c 3 -a
The -c 3 option will tell to ignore less important messages. And syslogd will only record important messages. If you want, 0 is the most important. 8 least.
The -a option will tell syslogd to archive the records. And clear the active records database every 24 hours.
All this will make your poppy a little faster.

Ps.
When I understood launchd, I ran into a small problem. When starting, the spawn-php program was called (yes, I know that it was stupid) and it caused php-cgi processes. When testing everything was good. But when loading nothing happened. Only from the tenth time it was possible to start processes. I do not know what it was connected with, maybe this is the problem in spawn-php, but it was solved in the way I described above, i.e. calling the process directly.

Links

Source: https://habr.com/ru/post/38078/


All Articles