how to make cronjob authentification? - php

I want every day to check my database and do some staff with it. Cronjob is amazing solution. But how to make authentification? Otherwise anyone can trigger that code at any time. Thank you.
For example usually person must insert login and password, get token and store it in cookies. After that you have rights to change anything. How to make it in cronjob.

add cron job in is such way so that php file run from CLI.Don't run it by sending http request.Command for running as CLI will be like php test.php
and add the following code in the php file so that it is only allowed to run from CLI:
if (php_sapi_name() == "cli") die('run only from CLI');

Related

why is my "at job" not executing my php script when created through a php webpage?

$output = shell_exec('echo "php '.$realFile.'" | at '.$targTime.' '.$targDate.' 2>&1');
print $output;
Can someone please help me figure out why the above line isn't doing what it's supposed to be doing? The idea is for it to create an 'at' job that will execute a php script. If I switch to the user apache(which will ideally control the at function when the php file is complete) I can run
echo "php $realFile.php" | at 00:00 05/30/17
and it'll do EXACTLY what I want. The problem is in the above snippet from my php file it will not create the at job correctly. when I do a at -c job# on both of them the job made from my file is about a 3rd the length missing the User info and everything. It basically starts at PATH= and goes down. Doesn't include HOSTNAME=, SHELL=, SSH_CLIENT=, SSH_TTY=, USER=. I assume it needs most of this info to run correctly. The end output (below)is always the same though it just doesn't have any of the top part for some reason. Let me know if you need more info. I didn't want to paste all of my code here as it contains job specific information.
${SHELL:-/bin/sh} << 'marcinDELIMITER0e4bb3e8'
php "$realFile".php
marcinDELIMITER0e4bb3e8
It doesn't seem to be a permission issue because I can su to apache and run the exact command needed. The folder the files are located in are also owned by apache. I've also resulted to giving each file I try to run 777 or 755 permissions through chmod so I don't think that's the issue.
I figured out a coupe ways around it a while back. The way I'm using right now is an ssh2 connect to my own server as root and creating it that way. No compromise as you have to enter the password manually each time. Really bad work around. The main issue is that apache doesn't have the correct permissions to do everything needed for the AT job so someone figuring that out would be awesome. Another option I found on a random webpage would be to use sudo through the php script, but basically the same minus having to reconnect to your own server. Any other options would be appreciated.
Reading the manual and logs would be a good place to start. In particular:
The value of the SHELL environment variable at the time of at invocation will determine which shell is used to execute the at job commands. If SHELL is unset when at is invoked, the user’s login shell will be used; otherwise, if SHELL is set when at is invoked, it must contain the path of a shell interpreter executable that will be used to run the commands at the specified time.
Other things to check are that the user is included in at.allow, SELinux is disabled and the webserver is not running chrrot.

run a php page once a day using NAS synology

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.

Get name of running process in php

I've been searching and can't find something clear, at least for me
I've made a newsletter and sent it with a mail script
Since it had a lot of emails, I said ignore_user-abort and let it work sending emails even if I closed the browser
Now I'd like to check the progress, I mean, if it has finished all the emails
I understand that there are functions like posix_kill and getpid but I don't know if they are the right ones to use
getpid gives me the id but how can I know the name of the script running? Maybe another script is running that I don't know
thanks a lot
Try the following:
Before the script start to send emails create a file by example emails.log
And when the script is done, remove it,
When the script starts again ask if exists, and if exists kill the script,
The only issue i see can happen is if the script dies unexpected or the page give a timeout the file will be not removed
For avoid this you can create a cronjob in your server and configure for run it daily, weekly or whatever you want, the shell script will look something like:
newsletter.sh
#!/bin/bash
if [ ! -f /tmp/newsletter.log ]
then
touch /tmp/newsletter.log
php /var/www/newsletter.php >> /dev/null
rm /tmp/newsletter.log
fi
Good luck!

How to set CRON using php and run php file with the help of it?

I have two php files. In one php file there is simple html form in which I created some drop down for select time and days for cronjob when user set time and day and submit form then all the drop down values stored in database.
Now with the help of this stored values I need to set cronjob and when cron will execute then it run another php file in which I write some code for generate xml file.
And this file run every time which time is stored by user in database.
By using Cpanel I can do that but I don't want to use cpanel.
Please give me some proper and working solution, I really need it.
Thanks in advance.
Use phps system() or exec() function to call the crontab command to replace or modify the existing crontab for the account the web server runs under. You might have to make sure that user is allowed to use the cron system, this depends on the operating system you use.
From within the crontab you can use one of two strategies to run a php script_
- call the php cli interpreter like any normal command, so something like: /usr/bin/php and give it the script to interpret. As an alternative you also can use shebang inside your php script and call it as a simple executable.
- use wget to call an url pointing to your webserver (maybe localhost) when you want to execute the script inside your web server.
This might act as a starting point for you to experiment with:
#!/usr/bin/php
<?php
// the time tokens to be fed into the crontab line
$time=array('10','*','*','*','*');
// the actual command to be executed
$command=sprintf("crontab -l | (cat;echo \"%s\t%s\") | crontab",
implode(' ',$time),
'/usr/bin/beep');
// execute command
$result=system($command);
// output result of execution
if ($result)
echo "Result: $result\n";
else
echo "FAILURE!\n";
?>
Works for me when called from CLI.

How to set interval for cron file using PHP

Im new to this.I have a cron file called xml.php and i want to run this file at a particular interval.This interval is set by admin on admin panel page.This interval will be stored in the database and i want to fetch that data from db.According to that the xml.php should run.If anybody know how to do this please help.Thanks in advance
Create a script that checks the DB for the last run of the file and the current time difference. If the interval is correct then run the xml script if not then exit. Use cron to run the new script every minute (so every minute it will check if the script needs to be run and run it only if it's time).
What you will need to do is save it as you wish in DB... but there is no way to dynamically change a crontab... What you can do tho is at point of saving to DB you can use PHP to manipulate the crontab using the details that are saved/to be saved you can construct an appropriate crontab entry - see this for more details: Use PHP to create, edit and delete crontab jobs?
It will also depend on the hosting company on their permissions if they will let you execute shell commands from within PHP.

Categories