Cannot execute crontab command from a php script...from shared host - php

I'm running an application where I need to execute the cron job, once a form is submitted,contains more than 1 lac records in a loop(its on-demand execution of cron job and deletes the job once finished).
I'm using php, I tried to configure the job through cpanel as a test and it works...the command was.."/ramdisk/bin/php5 -f /home/user/public_html/domain/cron.php -q", it worked fine.
But when I tried to run the same job with the exec() of php, like I wrote this whole command to a file (I do not know where the original crontab file is located otherwise could have written to it, please suggest a way to find its location) and ran that file like "exec("crontab /home/user/cron/Feed_cron"); ", in this cron is not working...
I doubted whether the crontab command will not work for particular user.. I tried it from the "root" user....which is the root user....tried the command via SSH or Putty and the response was "command not found". Please find a solution for this, also please give me an idea whether my concept works fine...

please suggest a way to find its location
Try
whereis crontab

Related

start a program from cmd with full permission

i have an application called app.exe it's create a file called account.txt in the same folder c:\ , the problem is that when i run the program from php exec function or cmd the account.txt file is not creating
i think the problem is from the permissions.
Run From: What happened
Manualy Executed and created the file account.txt
PHP exec Executed but it did't create account.txt
CMD Executed but it did't create account.txt
for php i use:
exec("C:\\windows\\system32\\cmd.exe /c START c:\app.exe");
for cmd i use:
START C:\app.exe
A program that requires elevation should specify so in its manifest. This would not help with the PHP issue though, especially if this is a web server using PHP. It is not possible to elevate without showing the UAC UI.
I'm not aware of a simple way to request elevation when executing something in Cmd. You might just have to start Cmd elevated (Right-click and "Run as Administrator").
PHP might be running as a different user but you gave us zero information about your setup so I don't know.
Without knowing anything about app.exe nor your Windows version or general configuration it is hard to give specific advise.
If you want to confirm that it as a permissions problem you can try running Process Monitor so you can see why the file operation fails.
You should be able to use the runas command
runas /profile /user:*admin user here* “*path to program here*”
you will then be prompted for that users password and after that it should run as admin.

Openshift cron doesn't run php file

I have 2 .php files in my application - book.php and weather.php. I create a file named "runscript" in /.openshift/cron/minutely. This file contents:
#!/bin/bash
php -f $OPENSHIFT_REPO_DIR/weather.php
This script send me message to phone every minute, it's OK.
Then I replace to:
php -f $OPENSHIFT_REPO_DIR/book.php
This script MUST send me message too, but nothing is happing. But if I just run this script by my webbrowser (go to the http://xxx-xxxxxxx.rhcloud.com/book.php) so I got my message. How is it possible? Magic?
Did you miss the #!/bin/bash part? That's needed to run the shell script.
For why your cron job is not executing, check the cron logs on OpenShift. You can find them at ~/app-root/logs/cron_*.log when you SSH into your gear.
Make sure your cron job is execuable with chmod, and has the shebang line as #gnaanaa says. Also check if you have one of the .openshift/cron/minutely/jobs.{allow,deny} files as they may cause cron to skip your job. (See the cron README for more information.)
And after your cron job is working, you can get rid of the wrapper script runscript and have cron call book.php directly. To do so, place book.php directly into .openshift/cron/minutely, make it executable, and add this shebang to it:
#!/usr/bin/env php
Hope this helps.
I use openshift aswell and executed a php file with a cron aswell.
#!/bin/bash
php ${OPENSHIFT_REPO_DIR}index.php
This executes the script normally at first sight. However no output was produced. The problem was, that all the required php files couldnt be loaded because the working directory was not the same as it would be when loaded by the webserver. Setting the working directoy in the php script itself will prevent this error and makes the script perfectly executable by the cron.
This should help some people to get their script running.

setting crontab is not working but crontab -l does via PHP file

I want to set a cron job using PHP file, but don't know where the problem is coming in my process.
When I wrote below (in PHP file and executed it on browser), it shows me the result. (It lists all cron jobs)
echo shell_exec('crontab -l');
But when I wrote below line, then it didn't set any cron job:
echo shell_exec('crontab /home/testsite/public_html/crons/crons.txt');
But If I run the same command (crontab /home/testsite/public_html/crons/crons.txt) via puTTY on my CentOS Dedicated Server, then it updates the crons list from crons.txt file.
I also tried passing -u as I saw on several stackoverflow questions:
echo shell_exec('crontab -u testsite /home/testsite/public_html/crons/crons.txt');
Can anyone help?
I'm not sure what exactly you are trying to do, and the way you are trying to change the crontab is not the best way either. Let me show you how I would do it:
First, get the user you are when executing the PHP by putting into the PHP script:
echo get_current_user();
Depending on the cron software you use (I use anacron, like most other ppl I guess), you go the cron config directory and put a single file for you up with write access:
touch /etc/cron.d/testsite
chown <user from above>:<user(=group) from above> /etc/cron.d/testsite
With PHP, you can directly read and write to that file now (file_get_contents(); file_put_contents()) and crontab will immediately use it.

unix `at` from php script, works in shell but not from web

I am having trouble executing a unix at job command from a php webscript.
the php with the at job looks like this:
$output=exec('echo "/usr/bin/perl /home/dir/test.pl" | /usr/bin/at now +1minute')
This will work fine from the shell command prompt. I can execute the php script, run atq to see that job has been set and the job belongs to root. Then wait and confirm that the test.pl script has run. The test.pl script performs a simple update to a database which I can check to confirm everything has worked.
When I execute the php script from the web, go back to my shell and run atq again. I can see that the job has been placed, this time the job belongs to apache, so everything seems fine. But after waiting for the scheduled time, the test.pl script does not execute which I can confirm after seeing the update to the db has not worked.
I belive this must be a permissions issue, but I am not sure. I tried giving /home/dir and the test.pl script ownerships to apache but this still does not work. I tried createing a symlink to /home/dir in the webroot folder but this did not seem to help either. I do not know apache permissions to well so I could be overlooking something.
here is the directory and test.pl ls -l ouput
/home/dir
drwxrwxr-x 2 apache apache
test.pl
-rwxr-xr-x 1 apache apache
Thanks for any help on this one.
If the job is being enqueued then we can discount the possibility that the apache uid is denied access to 'at'.
This then implies that the job is probably failing to execute (how do you know it is failing to run?)
Since you have root access, the next step would be to look at the mail file for the apache uid - or determine where mail for this uid is sent and look there: from the man page:
The user will be mailed standard error and standard output from his commands,
if any. Mail will be sent using the command /usr/sbin/sendmail
(If you don't find anything there, then you might want to try something like pwd | mail -s test apache or running at with -m to check that mail does get handled as it should)
Normally a daemon owner won't have any shell configured - did you set $SHELL before invoking at? (if this is the case and the mail is getting sent then there will be a corresponding error waiting to be read).
You've checked the permissions on the test.pl file and that does not appear to be the issue (assuming fACLs or SELinux restrictions are not being applied). Have you checked the permissions of the perl executable? Could test.pl be attempting to access other files which it might have permissions on?

Cron Jobs - Doesn't seem to execute

Looking through the internet at examples on how to run a php script from a cron job I've settled on this command:
php -q /home/myuseraccount/public_html/crontest/crontest.php
I tried running this from SSH and had success! But when I try this as a cron job nothing happens. I have it set to run every minute. Nothing happens. It should create a text file with the current time in it:
<?php
$file = '/home/myuseraccount/public_html/crontest/test.txt';
$now = time();
file_put_contents($file, $now, FILE_APPEND | LOCK_EX);
?>
It should also email me. It doesn't. I'm assuming it would email me no matter what, like if there was a failure?
It's not possible cron jobs are somehow disabled is it? - I'd assume I wouldnt even be able to set them up...
Is there any way, like from SSH, to test cron jobs?
I've never successfully been able to get cron jobs to work, so I don't know if it's a server issue or just ignorance.
--EDIT--
My cron log file seems to just show a bunch of LIST's and REPLACE's
Consider checking your logs for cron at /var/log/syslog
Please also make sure you can actually write to the file/directory (chmod permissions). Even further ensure that the crontab userlevel can write to the file/directory as well.
I just tried your file and it does execute fine for me, will attempt to do it as a crontab int he same way as you.
EDIT: Ran it as a cronjob and that worked as well, not sure what the issue is!
Apparently crond wasn't running....
#service crond restart
Stopping crond: cannot stop crond: crond is not running. [FAILED]
Starting crond: [ OK ]
Works now.

Categories