Global Library in Crontab PHP - php

Is it possible to include global $_lib, $_SETUP; in crontab?
I have a cronjob writting in php file in internet directory (/internet/mycrontab.php), but seem like the crontab throws error when I using $_Lib like in $_lib['db']->db_fetch_object($query).
The $_Lib working fine if I enter the url directly in browser www.myweb.dom/internet/mycrontab.php, and the crontab also working fine if I remove the $_Lib like in $_lib['db']->db_fetch_object($query) by using hardcode sysntax (primary).
If it is possible to include global $_lib, $_SETUP;, how do I correctly do that?
Many thanks for the help.

The problem is that the crontab environment and your web app environment are different things.
The cronjob is run by php-cli while the app is run by apache (or NGINX, whatever) php module.
You should probably evaluate to include your library into the crontab file.
include "/path/to/your/library.php";
$_lib = "whatever";
$_SETUP = "whatever";
Without having a proper look at the code that's the best I can suggest.

Related

Start and stop npm from PHP script

OK, a silly question. Is it possible to do start npm from a PHP script, via the browser? For example, by doing something like this?
// start.php
echo exec('npm run forever');
die();
And then, to stop node, like this:
// stop.php
echo exec('npm run stopforever');
die();
I've tried setting the absolute path in both the php scripts (/var/www/mysite.com/) and the package.json, and it didn't work.
If I try to execute the script via terminal (php -q /var/www/mysite.com/start.php), it works, but not when I try to execute the script from the browser. I'm guessing it has something to do with rights and privileges, so I might not be going about it the right way.
Is there even a recommended way of doing this? The reason I need this is to be able to start and stop the Node server at will, without logging to the console as a root user.
You have to use full path of executive files.
Try like this: (Worked on the Windows Server)
shell_exec('C:\Users\Administrator\AppData\Roaming\npm\pm2.cmd');
If you run php by nginx or apache (for example, visit url example.com/index.php), sometime you need to export the PATH
exec("export PATH=/usr/local/bin && run forever);

perl script is not running through cron

I need your help here.
I wrote one PERL script for PHP application which needs to be run for every 5 mins.
This script will call PHP program, which will fetch data from MySQL DB and will generate a excel report and will mail those reports to specific users.
Every thing seems to be fine when I ran this script manually with the command (perl reports.pl).
But when I set this Perl in a cron tab, nothing works and reports are not getting generated.
Details: perl script path /opt/app/deweb/web/EDI/Microsoft/reports.pl
this script will call PHP program (/opt/app/deweb/web/EDI/Microsoft/reports.php)
content of script
#!/usr/local/bin/perl
use Net::FTP;
use File::Copy;
use POSIX;
#errorreport = `php /opt/app/deweb/web/EDI/Microsoft/reports.php`;
print "#errorreport\n";
exit;
It is working perfectly when running Manually using command - perl reports.pl
No results, when set in CRON:
*/5 7-19 * * * /usr/local/bin/perl /opt/app/deweb/web/EDI/Microsoft/reports.pl
Please note that this crontab is under super user account named webserv and my login is having access to edit under this super user account.
I'm editing this cron tab using command :: sudo -u webserv crontab -e
I would check the following:
Does it run using sudo -u webserv perl reports.pl? If not, fix the problem for the webserv user (permissions or whatever) and it should work via cron too.
Does which perl using your login give you /usr/local/bin/perl? If not, change the path to Perl in crontab to what you got in which perl to fix the problem.
I found myself to be in the same situtation. After trying to find out the reason, I am almost sure about the reason this happens. Crontab does not have the same environment variables as you when running the script. You must be sure about paths. Try for example run your script like /perl-path /path-to-perl-script/script.pl outside the parent directory of the script and I am almost sure that your programm will not find some files. And as you call one php script from the perl script, it's possible to have the same problem with paths to your php script too.
So the solution is to use absolute paths and no relative.
Also at your perl script don't use php but /full-path-to-php for example:
#errorreport = /usr/bin/php /opt/app/deweb/web/EDI/Microsoft/reports.php;

using phps exec command to launch an application on the server

I'm attempting to run an application on the server, invoking it from PHP using the following code.
$application = "D:\\Program Files (x86)\\ScanBoy\\DM ScanBoy.exe";
exec($application);
Right now the application is 'run' however it crashes instantly. If I just run the application (by double clicking the exe) it runs and everything is fine.
When the application crashes the only error I get is
"{application name} has stopped working. Windows is checking for a
solution to the problem"
I have had this problem with running application via c# backend to a ASP.NET page. The solution there was to set the Working Directory. However in php / exec I am unaware of how to set this option.
Any help please?
You can either:
Use exec("cd myworkdir/ && D:\\Program Files (x86)\\ScanBoy\\DM ScanBoy.exe"); to change the working directory for that exec command (only)
Use the php chdir() function to change the working directory of the php process.
You can find chdir documentation here:
http://php.net/manual/en/function.chdir.php
You can chdir() to change current working directory

Can't run shell script from php web script

I am trying to run a shell script from a php script.
I have complete control of the environment (unix on mac), I should have all the permissions, etc. set correctly.
The web script is in /htdocs/
The shell script can be executed from anywhere so when I go to /htdocs/ in the shell, I can easily run it like this:
$ my_shellscript
.. but when my php script (which is located in htdocs) tries to call it:
shell_exec('my_shellscript');
I get nothing.
I have proven the script can be called from that location and I have temporarily granted full access to try to get it working somehow. I am going crazy, please help.
If you know of some other way of triggering a shell script via the web that would be fine.
Thanks in advance.
well i got few weeks same problem, the solution is to check if the apace has the permission to execute your script. You could also try to run the script in php cli.
Since it is a shellscript, it needs to be invoked with the path prefix. My guess is you need to do this:
shell_exec('./my_shellscript');
First thing: make sure php isn't running in Safe Mode
Next thing: Try running it with the exec() function and using the full path (e.g. /var/www/htdocs/my_shellscript)
Try doing
echo shell_exec('my_shellscript 2>&1');
which will capture the script's stderr output and print it out. If something inside the script is failing, this output would otherwise be lost when not being run interactively.

how to set up a php mysql site quickly?

I am looking for ways to setup a basic site quickly.
I have a basic site which works with a databasem, templates, css, js and so on.
No I want to be able to quickly set up a site. What shoudld happen when i start the script is:
ask for some variables
on submit:
create a folder in the webroot
copy the standard site to that map
create a database based on a default db
add the new site to my vhost file
restart apache
add the new site to my host file
start de basic site in a browser.
What is the best way to create this script? How can I accomplish this?
Here's a complete (untested) hack that does about what you said. If you're comfortable in php then use that. Pseudo script:
#!/usr/bin/env php
<?php
echo "Site setup v0.0\n";
if($argc != 2){
echo "Usage:\n script sitename\n";
}
//set vars
$sitename = $argv[1];
$src_folder = "/path/to/some/folder";
$template_db ="template_site";
//copy files
`mkdir $sitename`;
`cp -R $src_folder $sitename`;
//copy template db
$dblink = mysql_connect("localhost");
if(!mysql_query($dblink, "CREATE DATABASE site_$sitename; USE site_$sitename;"))exit(-1);
$r = mysql_query($dblink, "SHOW TABLES FROM $template_db");
while($row = mysql_fetch_array($r)){
$table = $row[0];
mysql_query($dblink, "CREATE TABLE $table AS SELECT $template_db.$table");
}
//conf and restart apache
$f = fopen("httpd.conf","a");//open for append
fwrite("<VirtualHost $sitename> bla bla </VirtualHost>");
fclose($f);
`sudo apachectl -k restart`; //you'll be asked for a password here
//open in browser
`open http://$sitename/`; //on mac anyway...
?>
Make the file executable with
chmod +x filename
Remember that to run scripts in the current folder you need to add ./. Like
./scriptname sitename
Also note the slanted quotes ` <- They start a shell command. The first line is called a shebang-line (yes like that old 80s band, or what was it..) and tells a shell what to use to execute the file. (Env is a utility program that kinda finds other programs, in this case php. Good if you want to run the script in systems where php has different install locations.)
Also please note that this script is just pseudo codeā€”it does not work! Don't run it before modifying it!
This is something you have to do on the server so you can use any language you prefer. It's just about copying files, append text to files and execute some shell commands.
Well, it depends on what OS you are using, if you use Linux you can create simple script to do this, it basicly should contain a sequence of commands to set this up.
#!/bin/sh
mkdir /var/www/sitename
cp -au /var/www/skeleton/* /var/www/newfolder
etcetera.
You can then launch your script by running it from command line with some parameters
./initiatesite.sh newsite.com databasename addwhateverparameteryouwant here.
More info on passing parameters to a shell script: http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html
You can write a shell script that basically accomplishes all of that.
So I guess you would have a web front end handled by php which takes some parameters, and have that php script invoke a shell script (via system call) on your server to set up your db, copy files, append some lines to vhost config, etc
If this is not for your own use but for clients, I would take extra care to make sure all input is untainted and secure first.
You can't do that in PHP unless you run PHP as root (bad idea).
Think about what you want to do specifically and how you would do it. Starting the site in a browser is a user-specific desktop action, restarting the server is a root action as is all the copying/editing/moving stuff.
Maybe a proper installer would be more suited towards what you're trying to accomplish. It sounds like you're working with Windows -- do you only want to provide an installation for Windows? For Linux you'd need a shell script, for example.
The best way to write this would be as a command line script, which you can write in PHP if you want. Use the $argv variable to get an array of the command line variables. Make sure that this PHP file is NOT web accessible.
Also, I would use SVN to do a export to the folder for the new site. This will ensure that you have the most up to date code and that it's a clean copy.

Categories