September 5th, 2010
admin
Sometimes you need to know paths to some system software that needs to be executed with your script. For example, in order to unzip a file without installing or using any specific PHP classes or extensions, you need to know where the unzip binary is located.
The solution is very simple. The only restriction is that you should have rights to use exec() or system(). Though, I don’t think there is any reason to deal with the paths if you are not allowed to run system applications. Here is the code:
$var=exec ("which unzip");
echo $var;
What does this code do? It catches the output of system which command to a variable. Then you can use this variable for your purposes. Of course, you should verify the value of the variable before using it. You may also need to echo something, if unzip is not installed.
This technology can be applied to any other system binary you need to know the path of. $var=exec (“which sh”); will tell you the location of sh, if you need to run any shell scripts with your PHP script, and so on.
If you need to start anything automatically in Linux, you should alter some files that are processed during system startup. There are lots of ways to do this; I will show you how to start Apache automatically. Note, that this installation of Apache does not allow to start it using chkconfig httpd on.
In order to setup automatic start, we will have to deal with /etc/init.d/rc.local . This is the file, that is processed on system startup and it will allow you to run anything you like. We need to start Apache using /usr/local/apache2/bin/apachectl start. Let’s add this string to the end of your /etc/init.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don’t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/local/apache2/bin/apachectl start
That’s all! Now your httpd daemon will be automatically started at boot. You can start any daemon this way. If you know any more methods to start daemons, please, add them in comments.
Sometimes you need to delete some archive files, that are older than the specified date. It’s simple enough in Linux. Here is the command that allows you to do this:
find /path/to/folder/* -mtime +7 -exec rm {} \;
Please, note, that you need to keep spaces between command arguments.
This command is suitable for folders that contain a large number of files. In order to force file deletion you need to add -f key.
find /path/to/folder/* -mtime +7 -exec rm -f {} \;
If you need to move these files instead of deletion, here is the solution:
find /path/to/folder/* -mtime +7 -exec mv {} /path/test-mv-to/ \;
Hope this helps you to deal with archive files and big folders.
Sometimes we need to kill a bunch of processes. For example, we have run a plenty of PHP scripts from command line, and have noticed there are errors in them. Let’s say the wrong script is called php_script.php. Then in order to terminate all processes associated with script name, you need to issue the following command:
kill `ps aux | grep php_script.php | grep -v grep | awk '{print $2}'`
What does this command actually do? It sends process IDs found by ps to the kill command. Then all these processes are killed. This command is used when any other methods, like killall -9 php are not acceptable for any reason.