Before I start with my progress, I’d love to share an awesome command I wrote the other day. I’m writing some intensive PHP scripts for my job that need to be run in the background and on multiple threads to increase speed and to provide the user with an asynchronous experience. I was going to use PCNTL, but it wasn’t compiled into the system I was on, so I ended up spawning off tasks as separate background scripts. The problem I ran into was that the website would grind to a halt because the scripts (usually 5 active at a time) would run on all four cores and take up 100% of the CPU. So what do I do? I need to run these scripts, but I need the website to continue to be operational.
My first choice was to play with the nice level of the process “proc_nice()” That worked at first, until I started dealing with 20-30 scripts at once. Then the *nix box decided to just throw EVERYTHING to the lowest priority just to keep the server up. Then I realized, I have four cores, why take up every core? After some research, I came across the “taskset” command which allowed me to specify which core my process will run on. Take a look at the following code:
exec("taskset -c ".($i % 2)"; php theScript.php > /dev/null 2>&1 & echo $!", $opt);
Assuming we’re in a loop with $i as the index, this will spawn the intensive processes into the background and balanced on the first two cores only. Also, if you read in “$opt[0]” you can return the pid of your new running process (if you needed to kill it later). Anyway, I was really proud of this line of code and thought I’d share it. Back to the studying.
The first chapter is all about PHP Basics. And as a PHP programmer for ~6 years, it shouldn’t be anything new. Here are some interesting things I didn’t know:
When building a Linux machine, sometimes it becomes necessary to set up cron jobs to transfer files or logs from one machine to another on a regular basis. If this is the case, I recommend setting up password-less SSH from one machine to the other. I decided to post about this here because it always took me a while to find the right commands or tutorial online to get it to work. Finally I found a great post from the Debian Administration team. Here is quick summary/tutorial.
Continue Reading »