Set cron job for absolute path - php

I am using MVC framework. Now I want to set up cron such that the URL "http://www.xyz.com/controllera/functiona" should be executed. what should i write in the path section for it.
I got something about "GET" command but it wasnt clear.
Can someone please help me out with it?

As you didn't specify any framework the only way to run this cron is this command
wget --spider 'http://www.xyz.com/controllera/functiona'
I assume you are using an MVC framework as controllera is in the url. If it was Kohana (2.3) framework I would have run it by
/usr/bin/php /path/to/index.php controller/method
Most framework has cli interface to run a controller method. Search for your framework.
See these links for different frameworks.
Zend Framework
Kohana 2 & 3
Codeignier
Yii

I don't understand the "module called cron" part of your question. I believe you are confused, cron is a service on Linux and other Unix systems configured thru crontab.
A crontab(5) entry is defined by a time and date and a command to run.
On Linux and Posix systems, you cannot execute or run an URL. Running something involves the execve(2) system call, which requires an executable file path (and arguments).
Perhaps you want to retrieve some URL using the HTTP protocol. You might use a command-line HTTP client, like wget or curl.
So perhaps the command you want to run in your crontab might be
wget http://www.xyz.com/controllera/functiona
but you could use curl
My guess is that you are confused, and don't understand well enough your question. Consider reading some material.
For example, to retrieve that URL once a day at 3 pm, you would have the following crontab entry:
# run everyday at 3 pm a GET HTTP request
0 15 * * * /usr/bin/wget http://www.xyz.com/controllera/functiona
Use the crontab(1) command to configure your crontab (which may contain several entries, and several variable definitions, so you may have to edit it).

Related

Accessing symfony 1.4 URL using CLI Terminal in Ubuntu

Can anyone tell me, how to access an action URL of symfony using terminal in ubuntu?
For Example:
http://mysymfonyproject.com/index.php/users/sendnewsletter
Same way, I want to set up an action class URL as cron job. Is it possible to set up cron jobs in symfony?
For Example:
http://mysymfonyproject.com/index.php/crons/sendnewsletter
I think a Symfony Task is what you should look into. There's a lot of built-in tasks, but you probably should write your own task and then call it from cli using a command:
php symfony yourTaskNamespace:yourTaskName
Then you can add such command as a regular cronjob task.
You can check detailed instruction how to create custom Symfony tasks at Documentation Page
Using tasks is the best solution (as Tomasz Madeyski suggested). Just be aware that the tasks work a bit differently than the web app (e.g. read about using sfContext in tasks)
You can also use curl to access your sf pages directly from CLI:
curl -sS http://mysymfonyproject.com/index.php/crons/sendnewsletter >> mylog.log
The -sS option will make curl silent unless there's an error. You can skip that if you don't need it.
The last part (>> mylog.log) will append the result of the request to the log file (it's best if you specify whole path to the log file)
As for setting the cron job it's fairly easy no matter if you use curl or sf tasks. Have a look around the web for a tutorial on setting the crontab, there's plenty of them out there.

Unable to delete Linux user using PHP and bash script

I'm able to delete linux users directly from the shell using the command ./del_user.sh
del_user.sh is as below
#!/bin/sh
userdel username
When I run this script from a PHP page, it doesn't work.
echo shell_exec('./del_user.sh');
Both the files are in the same directory.
What could be the problem?
del_user.sh does not take an argument so you're running the script on whatever hardcoded user is in the script. This won't work unless you modify the script to allow a command line argument.
While this task can be accomplished by PHP, this is certainly not a primary function and should probably be delegated to a more suitable application.
As an aside, you haven't stated what your goal is or what this script is supposed to do. Is this part of some user management application or is it simply a one-off for some small task?
Answer:
To enable this, you'd have to give Apache the ability to sudo so it can gain temporarily raised priveleges. You may have to do some googling on how to do this depending on your OS but this link provides some direction on how to do it in Ubuntu: Ubuntu Forums
I would also recommend against using a bash script as its not really necessary to do this. You could use a PHP script that accepts a command line argument. So in your main script you could have something like this: shell_exec('php /path/to/del_user.php username');
Then in del_user.php you'd have something like shell_exec('userdel '.$argv[1]);. More info on commandline arguments can be found here: $argv
Lastly, you could put it directly into your main script instead of using shell_exec twice. In other words, just use shell_exec('userdel '.$username); instead of calling a script. Since Apache will be able to sudo, it should be able to execute this directly.

How to run a command line script through a normal Zend Framework application

I am working on a ZF application that needs to run a command line script and then parse the results into something meaningful and return to the user.
I know there are various PHP functions, like exec and system, but I was wondering if there is anything built into Zend Framework that does command line scripting easily.
Even if there isn't a ZF specific function, what is the best function/method to use for running a command line script and then retrieving the results in PHP upon completion of the script.
I would write a Service which uses exec to get what you need. You can add some basic error handling there.
You can start an process under linux with this:
ZendX_Console_Process_Unix
But never tryed it..
We recently needed to do this (we use ZF too), hope this helps: http://www.kintek.com.au/web-design-blog/how-to-run-a-php-script-from-command-line/

How can I execute CGI files from PHP?

I'm trying to make a web app that will manage my Mercurial repositories for me.
I want it so that when I tell it to load repository X:
Connect to a MySQL server and make sure X exists.
Check if the user is allowed to access the repository.
If above is true, get the location of X from a mysql server.
Run a hgweb cgi script (python) containing the path of the repository.
Here is the problem, I want to: take the hgweb script, modify it, and run it.
But I do not want to: take the hgweb script, modify it, write it to a file and redirect there.
I am using Apache to run the httpd process.
Ryan Ballantyne has the right answer posted (I upvoted it). The backtick operator is the way to execute a shell script.
The simplest solution is probably to modify the hgweb script so that it doesn't "contain" the path to the repository, per se. Instead, pass it as a command-line argument. This means you don't have to worry about modifying and writing the hgweb script anywhere. All you'd have to do is:
//do stuff to get location of repository from MySQL into variable $x
//run shell script
$res = `python hgweb.py $x`;
You can run shell scripts from within PHP. There are various ways to do it, and complications with some hosts not providing the proper permissions, all of which are well-documented on php.net. That said, the simplest way is to simply enclose your command in backticks. So, to unzip a file, I could say:
`unzip /path/to/file`
SO, if your python script is such that it can be run from a command-line environment (or you could modify it so to run), this would seem to be the preferred method.
As far as you question, no, you're not likely to get php to execute a modified script without writing it somewhere, whether that's a file on the disk, a virtual file mapped to ram, or something similar.
It sounds like you might be trying to pound a railroad spike with a twig. If you're to the point where you're filtering access based on user permissions stored in MySQL, have you looked at existing HG solutions to make sure there isn't something more applicable than hgweb? It's really built for doing exactly one thing well, and this is a fair bit beyond it's normal realm.
I might suggest looking into apache's native authentication as a more convenient method for controlling access to repositories, then just serve the repo without modifying the script.

Running a Zend_Application from the command line?

Is there a blessed way to run a Zend_Application from the command line? That is, I want to run a shell script that invokes a Zend_Application, loads its configuration, and then calls a specific controller action OR run an arbitrary command line script with access to the applications configurations, models, etc.
I can think of a few ways to hack this together, but it seems like the kind of thing where there may be an official (but poorly documented) way of doing it.
here's one way: http://webfractor.wordpress.com/2008/08/14/using-zend-framework-from-the-command-line/
if there is actually a web server in the mix, you could also of course trigger w/wget or lynx... wget --quiet http://server/app/doTheThing

Categories