PHP script in /etc/cron.weekly - linux

I have a php script in my website folder that needs to be executed weekly.
I am on debian 6 (root).
How can I run this php script (in cli) weekly wihtout using crontab ?
I mean which kind of file should I copy in /etc/cron.weekly to run my php file?

Just use an executable PHP script with a "shebang" line:
#!/usr/bin/php
<?php
// Your PHP code goes here
echo "Hello World!";
?>
Make it executable:
$ chmod +x myscript.php
Test it:
$ ./myscript.php
Hello World!
The "shebang" (#!) tells the shell that this script is to be executed with PHP, found in /usr/bin/php.
Alternatively, you could write a small shell script that invokes the PHP interpreter with your PHP script:
#!/bin/bash
/usr/bin/php -f /path/to/script.php

Related

Multiscreening with Ubuntu

I'm running a Half-Life dedicated server on my VPS with following bash file named startserver in HLDS:
#!/bin/bash
screen -A -m -d -S hlds ./hlds_run -console -game valve -ip **ip address** -port 27015 +maxplayers 16 +map crossfire > /dev/null >&1 &
So I type ./startserver and my server is running. I'm not owner of this code, I just copied it. And I tried to make this code work for a PHP file and I created a directory named "test" and I have "test.php" code so that I can run my php file by typing "php test.php" but I need to run it on a screen too, because I'm not always gonna run the putty client.
I tried to edit the code in the directory where my php file exists and that's what I got inside:
#!/bin/bash screen -AmdS phper php test.php > test.txt >&1 &
I supposed to create a screen named "phper" and run the test.php inside. All files exist in the directory but I face with that error:
/bin/bash: screen -AmdS phper php test.php > test.txt >&1 &: No such file or directory
Am I doing something wrong?
The first line is wrong, if it is one line
#!/bin/bash screen -AmdS phper php test.php > test.txt >&1 &
First line of text file with executable bit set may begin with "#!" to set interpreter of the file, it is called Shebang. When you run the script and there is shebang, linux kernel will recognize it and start interpreter with the name of script as its argument.
You should not add anything after "/bin/bash", or it will try to parse it as his own parameters, and there is no bash parameter "screen" and no file with name "screen" (and with bash script inside) in current directory.
So, try put bash in shebang on first line and actual bash script from second line:
#!/bin/bash
screen -AmdS phper php test.php > test.txt >&1 &

php issue when running on linux with dot slash filename format

I have a script (file1.php)in php in linux
#!/usr/bin/php -q
<?php
echo "hello world" ?>
when i run in linux (redhat # bash shell)
> php file1.php
it works. my php is in /usr/bin/php and its version is 5.3.3)
but when i run
./file1.php
it says
'./file1.php' not present.
my application requires this ('./file1.php') model to work
on my other machine this file works with ('./file1.php') model
why is it so , is there any way to fix this ..
**/usr/bin/php -v works well
file permission (file1.php): -rwxr-xr-x 1 root root**
You should indicate how your file should be executed. In bash, it's done using a shebang
Try adding this line add the very top of your php script:
#!/usr/bin/php
<?php echo 'Hello world!';
This would tell bash to run ./file.php as php /fullpath/file.php
More info http://en.wikipedia.org/wiki/Shebang_(Unix)

Crontab running issue

I have 500 php files i want to make a crontab to run them automatically (they store XML data in my database) my problem is :
when I make a crontab for each php file it works fine. the command like this:
* * * * * php /home/username/public_html/codes/php0.php
But when I want to run a shell script including all my php files like this :
* * * * * bash /home/username/public_html/codes/php.sh
it does not run.
php.sh:
#!/bin/sh
php php0.php
echo php0
php php1.php
echo php1
php php2.php
echo php2
.
.
.
Is it possible to wrap php files with bash script? and if yes why does not work am I miss something?
You are probably not running in the directory you believe you are.
So replace temporarily #!/bin/sh with #!/bin/sh -vx
and add a
pwd
at the beginning of your script (that is, the 2nd line)
Then perhaps add a
cd /home/username/public_html/codes
or maybe define a variable and use it:
mycodedir=/home/username/public_html/codes
php $mycodedir/php0.php
echo php0
etc...
I suggest to read the advanced bash scripting guide (even if it does have weaknesses).
At the moment, you have relative paths in your php.sh. The following line
php php0.php
means that php will look for the php0.php file in the current working directory. In a cron job, the working directory is usually /, i.e. the file system root. To check what the working directory really is, you can use the pwd command.
So your php.sh script looks for a file php0.php in your file system root, but the file is in /home/username/public_html/codes/.
If you use absolute paths in your php.sh, the current working directory does not matter. So simply add absolute paths in your php.sh. The file will look like this:
#!/bin/sh
php /home/username/public_html/codes/php0.php
echo php0
php /home/username/public_html/codes/php1.php
echo php1
php /home/username/public_html/codes/php2.php
echo php2
.
.
.

How to create file with instruction of commands?

I have a file name myFirstFile that contains certain commands.
But I am not able to excecute them.
If I want to execute this as a program, which code should be implemented?
If you want to execute your program, it should start with:
#!/bin/sh
It's the generic script file "header". It indicates that the script is a shell script (if it's bash script you should have #!/bin/bash, etc.). If you want to execute it, you should call chmod +x ./myFirstFile to give privileges to call it as program, and then you can start your script normally: ./myFirstFile.
Make this file executable* and give it *.sh extention like:
"myFirstFile.sh"
Than run it from terminal (or crontab - it can do things for you when you sleep :) ) like:
cd directory/you/have/that/file
sh ./myFirstFile.sh
*Im not shure that making it executable is the most secure thing you can do. All my sh scripts are and I never digged into this issue, so make sure its cool
Also make sure you have "#!/bin/bash" in first line - sometimes it helps (dont know why, Google it)
edit: for example my script for starting Minecraf server looks like this
start.sh
#!/bin/bash
BINDIR=$(dirname "$(readlink -fn "$0")")
cd "$BINDIR"
while true
do
java -Xmx3584M -jar craftbukkit.jar
echo -e 'If you want to completely stop the server process now, press ctrl-$
echo "Rebooting in:"
for i in {5..1}
do
echo "$i..."
sleep 1
done
echo 'Restarting now!'
done
You have to make the file executable:
chmod +x myFirstFile
Then you can execute the commands in it:
./myFirstFile

Cron not passing params to PHP script

I have a cron job set like
php /home/novinarb/public_html/index.php --uri="cron/24satahr"
but the 'uri' param doesn't get to the php script at all. I also tried without the -- in front of uri but still nothing. Any ideas?
A more robust method would be to accept command-line arguments in your PHP script with getopt() or $argv and making it executable. An example with $argv called script.php:
#!/usr/bin/php
<?php
if (isset($argv[1])):
echo $argv[1];
endif;
?>
Make it executable:
chmod +x script.php
And execute:
./script.php "cron/24satahr"
Will output:
cron/24satahr
I was facing the same problem but was able to fix it after reading the manual entry for php.
Initially I had something set like:
/usr/bin/php -f /path/to/php/script.php -c 1426 >> /tmp/script.php.log 2>&1
I was able to fix it by changing the line to:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
As per the manual entry the syntax is:
php [options] [ -f ] file [[--] args...]
Also,
args... Arguments passed to script. Use '--' args when first argument starts with '-' or script is read from stdin
Going by that, my cron command becomes:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
and it works!
Is the php script running at all?
I suspect you need to provide the full path to php in your crontab line. Even though cron jobs run as you, they don't have any of your login environment set up; this means they don't have your $PATH.

Resources