Cron job don't execute specific php script - cron-task

I setup a cron job (in Virtualmin based of Webmin) for execute a simple test script and it work well.
The cron command used is:
/usr/bin/php -q /home/myuser/domains/mysite.com/public_html/mailtest.php
The url is:
www.mysite.com/mailtest.php
The content of "mailtest.php" is:
<?php
$to = "your#mail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "any#any.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Now I tried to execute another script and I have of course modify path and file name who is simply:
/usr/bin/php -q /home/myuser/domains/mysite.com/public_html/myfolder/myscript.php
But here I don't know why my script is not executed. When I go to his related url
"www.mysite.com/myfolder/myscript.php" the script is well executed.
The content of "myscript.php" is:
<?php
require_once(dirname(__FILE__).'/includes/includes.php');
$mails = POP3Mailer::ProcessMails();
require_once($BASE_PATH.'/includes/footer.php');
?>
Somebody have an idea why the cron won't fire with "myscript.php" ???
Thank for your time

I'm not really sure about it. But maybe you should check permissions on this file.
Use the chmod command.

Cron is working in very basic environment. Create new file. Make it executable (chmod +x) and add it to cron. Add #!/bin/bash at the begining of the file and than your script via /usr/local/bin/php.
Your file should look like :
#!/bin/bash
/usr/local/bin/php /url/to/your/script.php

Related

VSQL is generating output file using perl script but not in cronjob

i have created the perl script in which i am connecting to vsql and running queries. when i run the script mannually, it is creating output files as expected. but when i set this script in crontab then output file is not generating. perl script is given below
#!/usr/bin/perl
$timenow = `date "+%H_%M"`;
chomp($timenow);
$cmd = "/opt/vertica/bin/vsql -d xxxx-U xxxxx -w xxxxx -F \$'--FSEP--' -At -o dumpfile_" . $timenow . ".txt -c \"SELECT CURRENT_TIMESTAMP(1) AS time;\"";
print "$cmd\n";
system($cmd);
and below is the contab entry
*/2 * * * * /usr/bin/perl /tmp/test.pl
can somebody please help what i am doing wrong ?
Your output is written to a file called dumpfile_[timestamp].txt. But where is that file?
Your command contains no directory path for that file. So it will be written to the current directory. The current directory for a cronjob is the home directory for the user that owns the cronjob. Have you tried looking there?
It's always better to be more specific about which directory you want the files written to. Two ways to do that are:
Change to the directory before running your command
*/2 * * * * cd /some_directory_path && /usr/bin/perl /tmp/test.pl
Include the full path in your Perl program
-o /some_directory_path/dumpfile_" . $timenow . ".txt
Update: Ok, take two.
Who owns this cronjob? Any output from the cronjob will be emailed to the owner. And there's definitely output as you have a print() statement. Any errors will be included in the same email. Do you get that email? What's in it?
If you don't get the email, you can change the address that the email is sent to by adding a MAILTO parameter to the crontab. It will look like this:
MAILTO=someone#example.com
*/2 * * * * /usr/bin/perl /tmp/test.pl
Bear in mind that the server might not be set up to send external email, so you might need to use the local mail program on the server.
If you can't get the email to work out, you could look for cron errors in /var/log/syslog (or, on a systemd system, try journalctl _COMM=cron).
The print() output is going somewhere. You need to track it down.

PHP Output correct in terminal but not in browser

I just started working with shell_exec in php and stuck at this point.
Below is my php script which runs in terminal correctly but not in browser.
<?php
echo shell_exec("ssh -tq root#192.168.31.5 \"whoami\"");
?>
And output in terminal is
$ php /var/www/html/monitor/ssh.php
root
But in Browser,
Interesting thing is just whoami works like a charm
<?php
echo shell_exec("whoami");
?>
any suggetion is appriciated. Thank you!
EDIT :- USING OB_START() and OB_GET_CONTENT
<?php
ob_start();
echo shell_exec("ssh -tq root#192.168.31.5 \"whoami\"");
$out1 = ob_get_contents();
ob_end_clean();
var_dump($out1);
?>
OUTPUT IN TERMINAL :-
php /var/www/html/monitor/ssh.php
string(6) "root"
OUTPUT IN BROWSER (CHROME) :-
string(0) ""
That's because in CLI you're executing the script as the user from SSH (root in your case) but in browser, the one executing the script is your WebServer (apache/nginx). For you to get root as output in browser you might want to have a look at ob_start ob_get_contents ob_flush functions.

What's in teamcity custom_script.cmd

I'm trying to dig into the depths of teamcity to get a better understanding of what its doing under the hood(and improve my own build knowledge). I noticed that when I run a build step it then executes its own .cmd which I presume wraps up the msbuild scripts. The problem is that whenever I look in the directory specified the file doesn't exist as I'm guessing it creates, executes then deletes almost instantly. Any suggestions on how to access the file? or what's inside?
Starting:D:\TeamCity\buildAgent\temp\agentTmp\custom_script5990675507156014131.cmd
A temporary file is created by TeamCity when you run add a Command Line Build Step with "Custom script" as runner.
The content of this file would be the Custom script you specified inside the user interface.
The produced output would be:
Step 1/1: Command Line (1s)
Starting: D:\TeamCity\buildAgent\temp\agentTmp\custom_script2362934300799611461.cmd
in directory: D:\TeamCity\buildAgent\work\c72dca7a7355b5de
Hello World
Process exited with code 0
In case anyone is wondering about this still, you can force echo back on.
Put as the first thing in the custom script
#echo on
this will undo the silent commands teamcity defaults to.
I looked around for a while but there seems to be no configuration variable in TeamCity allowing to keep generated files. Now if the commands executed take some time, e.g. more than a couple of seconds, you could just open the temp directory in explorer and start hitting F5 (refresh) from the moment a build is started until you see the .cmd file appear, then be quick and right-click it and select 'Edit' to open it in a text editor. If that is too hard you can try with the solution presented here: create a Powershell script with code like this:
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "D:\TeamCity\buildAgent\temp\agentTmp"
$watcher.Filter = "*.cmd"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
$action = { $path = $Event.SourceEventArgs.FullPath
Add-content "D:\log.txt" -value (Get-Content $path)
}
Register-ObjectEvent $watcher "Created" -Action $action
Register-ObjectEvent $watcher "Changed" -Action $action
while ($true) {sleep 1}
and run it. When the build starts and creates a cmd file, the powershell script will copy the content to d:\log.txt. This will still not work for very short-lived scripts though. In that case I'd just make the script last longer by adding something like
ping 127.0.0.1 -n 5 -w 1000 > NUL
which will make it last at least 5 seconds.

How to use LuaDoc with LuaForWindows

The question is all in the title : how to use LuaDoc with LuaForWindows ?
In my Lua installation, I have a luadoc_start.bat but the command windows closes as soon as it opens.
From there I don't know what else can I do.
Any help ?
Thanks
For using luadoc in Lua For Windows, the command is like this:
luadoc_start.bat path\to\lua\file\name.lua
which is to be done in either the command prompt window, or powershell.
I get a doc file to properly generate but how to do it for the whole project? I understand there is a -d argument but I am not sure how to use it, none of my tries where successful.
For this task, you'll need to write a shell script. Here's a small powershell script.
$files = Get-ChildItem .\
foreach( $file in $files ) {
luadoc_start.bat "$file"
}
Where, you have to cd to the path\to\lua\file directory and run this PS1 file.
Just append pause line to luadoc_start.bat and you will see help screen.

Cron Job - Could not open input file:

I have set up a php file to run that just echos hello.
<?php
echo hello;
?>
My cron job looks like this:
/usr/local/bin/php -f “/home/username/public_html/mls/test.php”
when my script runs i get a confirmation email that says:
Could not open input file: /home/username/public_html/mls/test.php
I don't know what is causing this. I am using godaddy's virtual private server with cpanel x installed. I have used the ssh to set permissions 777 on folder and file and still can not get it to run.
Any advice would be helpful. Thanks.
For some reason PHP cannot open the file. Try replacing /usr/local/bin/php -f with "ls -la" to try to crib some more information. Remember to NOT quote the file name in the crontab: php -f filename.php, not php -f "filename.php", unless it contains spaces -- and then it's better to use single quotes.
Possibly, try "ls -la /home", "ls -la /home/username", "ls -la ~/public_html" and so on.
Also try appending
2>&1
to the command line, in case only stdout is mailed to you (I don't really think so, but being sure costs little).
One other possibility
The crontab as it is refers /home/username/public_html/mls/test.php - that is, a public HTML directory inside username's commonest value for a home directory.
It is possible that the cron job is either not running with the appropriate user and privileges, or that the user it "sees" is actually a virtual user - there is no "/home/username" at all - and the "home directory" is elsewhere, possibly even existing just as long as the cron job runs. In this case the solution might be to refer to
~/public_html/mls/test.php
or, as described above, to first run a command such as pwd or ls -la to determine exactly where the cron job's current working directory is.
If this, too, fails, then another workaround could be to invoke the PHP HTTP handler via curl or lynx:
/usr/bin/curl http://www.thishostname.com/mls/test.php
Possibly using either some environment variable or curl header or _GET option to authenticate to the script as the cron job, and avoid it being accessible from the outside.