Kyle Piontek
Kyle Piontek

Reputation: 185

Running PHP script from command line

So I'm working with a designer on a website in PHP/MySQL and there are a few scripts that he would like to have to make life easier for him. He is pretty comfortable using the command line for stuff like git, SASS, node, etc. I would like him to be able to run my scripts like he would run a program, instead of running it through PHP.

So instead of this:

php /path/to/file/create_module.php module_name

I would like to do this:

myscript create_module module_name

Is it possible to do this with PHP on an Apache server? I know I will most likely have to modify the server to interpret it properly, which is fine. I just don't even know where to begin, and couldn't find what I needed on Google.

Upvotes: 3

Views: 3850

Answers (4)

Matt Clark
Matt Clark

Reputation: 28639

Your best bet would to be to create an alias.

So an alias of myscript would actually point to the command: php /path/to/file/create_module.php and then any extra arguments will be passed as typed.

In command line, do the following:

cd /etc/
nano bash.bashrc

At the very bottom of the file, add this line of text:

alias "myscript=php /path/to/file/create_module.php"

BASHRC is a script that is run on user login, so the alias will be recreated every time the user logs into the system.

Upvotes: 3

dualed
dualed

Reputation: 10512

Another option: Shell script wrapper

create a new text file in /bin or another directory in your PATH, name it how you would like to invoke your script and give it this content

#!/bin/bash
cd /path/to/your/php/scripts/folder
php script.php $*

don't forget to

chmod a+x /path/to/bash/script

The advantage of this is that your PHP script is run in the right directory where it may expect other resources to be that it depends on.

Upvotes: 1

Daryl Gill
Daryl Gill

Reputation: 5524

On A Linux Solution:

/usr/bin/php /path/to/file.php

On a Windows Solution:

C:\Path\To\PHPExe C:\Path\To\phpfile.php

Upvotes: 0

sberry
sberry

Reputation: 132138

I am not sure what you are looking for myscript to do, but to run a php script via the command line without specifying the php binary, just add a a shebang, like

#!/bin/env php
<?php

// The above expects env is in /bin

$foo = "bar";

Or the full path if you like

#!/usr/bin/php

Upvotes: 2

Related Questions