run a php page once a day using NAS synology - php

have a php page called cronEmail in the web folder. It incudes the code to end an email to specific users on the website. I want it to open the page once a day and send the email. The page has only php and MySql code to read the recipients of the email.
I am trying to use Task scheduler in the control panel to run the page. I create a user defined script and in schedule I set the time to a certain time and to only run daily once a day.
In the rum command i have tried numerous ways to run it on the time but every time it just passes and does nothing. an example of what I put in for the script is
/web/cronEmail.php OR
chmod 755 /volume1/web/cronEmail.php
There are only two of a many can anyone point me in the right direction
Thanks a million

Seems like you want a cronjob, in the terminal open cron with:
crontab -e
then at the bottom of the file place this
0 4 * * * php /url/to/folder/cronEmail.php

Not sure if you found another solution, but this is what worked for me. If you put the following in the "Run Command" section, it should work:
php /volume1/web/cronEmail.php
You can also create an error log by doing the following:
php /volume1/web/cronEmail.php>> /volume1/web/errors.log 2>&1
If you don't use the php at the beginning, and open up the error.log file that is created, what you'll see is that the Task Scheduler doesn't seem to know that it's looking for PHP, and doesn't recognize the script.

Related

Write LogFile with date on running service

I've created a service with reactphp which runs and does some stuff. It is started as a daemon so all output should be logged in a file.
This log file should be named 'foo-log-file-$(date "+F")'. I want to have a single log file for each day.
Problem:
As mentioned the script runs as a service, without stopping. The starting call for the script is therefore only done once.
php my_script.php >> /var/log/bar/log-file--$(date "+%F") 2>&1
So everything which is printed to the console from this script is saved into the file, but the file is only created with the date-string when it was called and is not updated with a new date.
Question:
Is it possible to solve this without writing the log logic in the php-script? Can i handle this requirement with bash?
FYI
The answer of #fedorqui was a good approach, i solved it with a cronjob, which copies the file to a different one and empties the rest.
You cannot use move, cause the through the running service, it is open all time and you get the error:
cannot move 'foo.log' to 'bar.log': Text file busy
So i cp it and clear the old one with:
cp foo.log foo.log.$(date +"%F");
cp /dev/null file.log;

Run Cron Job on PHP Script, on localhost in Windows

I have a php script and want to run it on an schedule. I am using local web server on windows (WAMP server) and need a way to run my_script.php every 10 min.
How to run a cron job on a PHP script, on localhost in windows?
recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.
By the way I am sharing this for anyone that is on the same thing.
You will need a shellscript to run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exe and run your php script (here called as my_process.php)
shellscript.vbs
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
script.bat
"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"
Now, we are ready to set the Windows Task Scheduler to run shellscript.vbs at the required time interval:
Open Task Scheduler from windows Start menu
Go to Action menu and hit Create Task...
in General tab, fill the Name and Description fields as you want
in Triggers tab, hit New button.
from Begin the Task dropdown, select On a schedule and choose Daily
from Advanced settings section, select Repeat task every as you want and set for a duration on Indefinitely.
on Actions tab, from Action dropdown, select Start a program.
on the Program\script box, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
leave Add argumentts (optional) section empty.
in Start in (optional) box, enter parent directory of shellscript.vbs like C:\path\to\.
Hit upvote on this tutorial :) Have fun.
To setup a Windows machine to run cron.php at a specific time follow the specific instructions below. This can be useful if you are not familiar with Linux/Unix, or if your web host does not offer the ability to run cron jobs; you can run them remotely from your own computer.
Note: These instructions were written for Windows XP but should be similar in other versions of Windows.
https://www.drupal.org/node/31506
If you use answer from Trix and get same problem as Metafaniel:
I have a problem, with this procedure, the task is beinge executed,
however my php script it's not running, I got the "Open with"
dialogue. If I see the properties of my task, it states: ActionName
C:\Windows\system32\OpenWith.exe even if I set it to the vbs file as
you suggested. What am I doing wrong?
you should use this:
Instead "enter path to shellscript.vbs like C:\path\to\shellscript.vbs." use "C:\Windows\System32\wscript.exe"
Instead "leave Add argumentts (optional) section empty" use (with quotes):
"C:\path\to\shellscript.vbs"
After many failed attempts at using Windows Task Scheduler this is the method that worked for me on my Windows 10 Professional machine. I just skipped adding a .vbs and .bat file and posted the PATH to the script file directly.
Under "Actions > Settings" Where it gives the option to browse for a "Program/Script" that's where I put the full PATH AND Filename to the script I wanted to run WITHOUT quotes.
C:\Server\Scripts\script.py
Then in the same area under "Actions > Settings" right below the text box where you just entered your "Program/Script", it has the area that says "Start in (optional)". That's where I put the PATH to the program that runs the script (ie: python.exe in this instance). But I did not specify "python.exe" directly I used the PATH ONLY without quotes like shown below.
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
If you installed Python directly without Visual Studio it will be under a different path probably something like "C:\Program Files\Python\Python37" or something similar if I remember correctly...
Little bit late but just for someone who don't want it to be lengthy and don't want to waste time in testing (as the aim is to host it to server), can do one thing by adding http content refresh in the html meta tag of that php file and add the required timer to it and run it in the browser.
NOTE: You have to make the system remain open untill you don't get the required output.
Again I am saying this is not the perfect and proper way but ya can save someone's configuration setting time.

sending email periodically through php script

I am working on an automatic monitoring system. I have made a .NET application which have MySQL database. For this I developed a normal ADMIN Panel where admin can log in and get necessary reports coming from various queries fired on the database. There is also a "summary Report" in the panel which is just the rough weekly summary. Now What I want is, I want this report (all text) to get sent automatically to some email "xxxxx#xxx.com" with a seven day period. I have used some PHP scripts previously to send email on submit button click. Like the one below.
<?php
if(isset($_POST['isPost']))
{
$header="From:".$_POST['customer_mail']."\r\nName:".$_POST['name']."\r\nCity:".$_PO ST['city'];
$subject = $_POST['title'];
$message="From:$_POST[customer_mail]\r\nName:$_POST[name]\r\nPhone:$_POST[city]\r\n \r\n\r\n\r\n".$_POST['details'];
$to = 'xxxxxxxxx#xxx.com';
$send_contact=mail($to,$subject,$message,$header);
if($send_contact)
{
echo "<h6 style='text-align:center;color:green'>Sent Successfully</h6>";
}
else
{
echo "<h6 style='color:red'>Error sending e-mail'</h6>";
}
}
?>
this is normal mail sending script which works fine. But for the above purpose I want, Can anyone help me to set this action periodically and automatically or point me in the right direction on how to do this. I have googled for such php script but no satisfied results.
~ Regards
You can do this with cronjobs.
A long running process which executes commands at given times / dates / intervals.
Depending on what system you are, there are different methods.
If you have the script on a webserver someone is running for you / Webhost service
Ask the system administrator to run the script with a cronjob. Or search for any help documentation if you can setup this yourself in any admin-panel. Ask your webhoster /system admin for more information.
If you have the script on your own local machine:
UNIX
Try reading about crontab on how to run the php script, or any script for that matter.
For example type crontab -e and add the line below in your crontab, the cronjob will run your script every hour:
00 * * * * /usr/local/bin/php /home/Low-pointer/myscript.php
Here is some more information if you want to play with the intervals
Windows
If you use Windows you can use scheduled tasks to run the php command.
For example add the following command to the scheduler: C:\wamp\bin\php\php.exe -f C:\wamp\www\my_script.php
You can also use web cron services (Google it) these are online services which run a page (for example on your webserver) on designated times. For free or paid.
You can use Cpanel to send schedule emails through the cronjob(if you are using it).
Once you open cpanel theere would be crontab system.
Add your current code to a file(xx.php) and add this command to crontab in cpanel ,
/usr/bin/php -q /home/public_html/xx.php
like everyone have already said you must use cronjob to make your task.
I assume you use a Linux OS as your production environment.
So you need:
1) A php endpoint ( eg. www.mywebsite.com/emailsend.php ) OR a CLI php script that when called send the email.
2) The correct crontab rule
Here below an example of a simple shell script ( mailsend.sh ) that call an endpoint using CURL and save an html file with an eventual response given by the webserver
#!/bin/bash
curl http://www.mywebsite.com/emailsend.php -o /path/to/my/mailsendreport/"$(date '+%Y-%m-%d-%H-%M-%S')".html
To add a scheduled task to cron
crontab -e
then add a rule like below
50 23 * * 6 sh /path/to/mailsend.sh
What "50 23 * * 6" means? It means that every sixth day of the week, in the 23th hour, in the minute 50 your sh script will be called, and then your web app and the email is sent.
Once I wrote a small doc about this you can see it here
Cheers
You're looking for "cron job".
Edit: Or since it sounds like you might be on Windows, "Scheduled Tasks"

How to run a script under CRON that outputs a CSV file and is then emailed

I need to run a script that queries my DB and outputs the results as a CSV file. I have the script working to produce the CSV I now just need to figure out how to schedule this task for 2am and email the output CSV to a list of email addresses using PHP.
Any help or ideas would be appreciated.
you edit your crontab and simply schedule what you want to happen
e.g.
$ crontab -e
to edit your crontab. Add an entry to run your script
MAILTO=me#example.com
00 02 * * * cd /here && runMy2AMScript
And that's about it. your script does all the work, cron just calls it. For more information about crontab - use man crontab or man 5 crontab.
If your unclear how to schedule sending an email - that's 2 tasks scheduling (which is above) and a script that sends an email - which you can do any number of ways (such as using swiftmailer. Pick a technique and ask another specific question if you are stuck).
I don't have much idea about cron. But once I tried firing mail using cron. My php site folder is home/USER/www. So this is what I did:
1. created a txt file: cron.txt
2. placed cron required informtion : 29 16 * * * php /home/USER/www/test_cron.php
3. ( test_cron.php contains code for a dummy mail )
4. opened terminal and executed command : crontab cron.txt
and its been two month and I daily get an email on 4:29pm
Hope this information will help you.
Any help or ideas would be appreciated.
More information would be appreciated. Where do you run into problems? Is it in the scheduling? If so, do a Google search for "Scheduled tasks" in case the webserver is running Windows, or "Cronjobs" if the webserver is running Linux, Unix or BSD.
E-mail a file is rather simple if you use a library; I would suggest looking into attaching files with Swiftmailer.

Create cronjob in kloxo to run .php file every x minutes

I am trying to add a cronjob in the kloxo based user interface,
So i navigate into cron scheduled tasks -> add standard -> Select x minutes, select 'all' in all other fields (hour, day, week, month, ..), then add the path in the last field: /home/piscolab/public_html/keepyourlinks.com/includes/boot.php
But the file is never executed,
what am i missing?
find the problem: check the cron log. if you dont know where your cron log is going to check the syslog. if you cant find anything there kill the cron proccess and start a new one with the -L /yourlogpath/ parameter specified. wait till the job is supposed to run and check the log after.
fix your problem or come back here to get help
as i see you dont specify what program should run the *.php file. change that to whatever program you want to run the php file.
if you use linux and the gui/webgui sux, fall back to shell. cron jobs are defined within /etc/spool/crond/crontab/root (or something similar to that, cant remember the exact path right now, but using TABSTOP will do the job for you). the crontab syntax can be seen here and here. Cron newbies go here as well.

Categories