Executable PHP files

I have a PHP script that I will be running in one of three ways:

  • interactively, from the Linux command line
  • as a cron tab, on a Linux box
  • as a web page, with Apache and mod_php

In order to do this I found that I can do the following:

#!/usr/bin/php -q
<?php 
@ob_end_clean();

if (isset($argv)) {
	define("SHELL",true);

	$options = getopt("c");
	if (isset($options['c'])) {
		echo "Running as a cron job ...\n";
		define("INTERACTIVE",false);
	} else {
		echo "Running interactively ...\n";
		define("INTERACTIVE",true);
	}

} else {
	echo "Running via Apache...
";
	define("INTERACTIVE",false);
	define("SHELL",false);
}

The general idea is:

  1. Line 1: tell the Linux shell that the command, when executed directly, should be interpreted by php, and not the shell that you’re running in (“-q” tells PHP to suppress any HTTP headers).
  2. Line 3. If the script is running via Apache then tell it to remove the first line which has already been output. Use the @ to suppress warnings as ob_end_clean isn’t available when running from the CLI.
  3. Use the $argv array to determine if we are running via CLI, it’s not set when run through mod_php
  4. When executed from cron, we will pass a parameter “-c” to indicate it should run in “non-interactive” mode, use getopt to read the parameters and isset to check if the “-c” parameter was passed.
  5. Now, the SHELL and INTERACTIVE constants can be used to determine if we are in a shell or Apache, and interactive or cron-mode, respectively.

Finally, don’t forget to chmod the script to make it executable.

chmod 700 script.php

Skype command line switches

There are a few Skype command line switches documented on the Skype support site:
https://support.skype.com/en/faq/FA171/can-i-run-skype-for-windows-desktop-from-the-command-line

However, it seems that the list isn’t complete. From a post on the Skype community site (the post doesn’t seem to be available any more) I found the following additions:

This will start Skype with the specified account name and password:
"C:\Program Files\Skype\Phone\Skype.exe" /username:your user name /password:your password

This will start a “portable” Skype installed at “path to skype.exe” with all data being saved in the “Data” folder:
"path to skype.exe\Skype.exe" /datapath:”Data” /removable