• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

How to use cronjobs to execute a php file?

PlayCayP

Basic Pleskian
Hello,

I tried to add a cronjob (sheduled task) via the Plesk 11 Interface, to execute a php file within my webshop.

But unfortunately I can not get it work.

The following command I have used:

php -q /var/www/vhosts/domain.com/httpdocs/shop/my_cron.php

Via email it showed me this error: Could not open input file: –q

Which command should I else use, to execute that php file? It just needs to be exeuted, like if you would open it manually via your browser.

It would be great if you can help me with that.

Thank you!
 
You would typically use wget or curl to achieve this. e.g.

wget -q -O /dev/null http://www.yourdomain.tld/page.html

The -q option tells wget to be quiet (I think this isn't needed really because...) the -O /dev/null tells wget to send the ouput to nowhere

You can do something similar with curl.
 
Okay thank you!

So it is better using wget to open the site from external sources, instead of executing the php file internally from /var/www/... ?
 
Hello,

I have tried several commands to execute a php file located at my live website, but unfortunately none of them work (nothing happens). But if I start them manually (via a browser), then it works.

Does any one have an idea, why the commands do not work? Which could I use instead?

GET http://www.domain.com/file.php
wget -q -O /dev/null http://www.domain.com/file.php
php -q /var/www/vhosts/domain.com/httpdocs/shop/file.php

Thanks in advance!
 
Hello,

I have tried several commands to execute a php file located at my live website, but unfortunately none of them work (nothing happens). But if I start them manually (via a browser), then it works.

Does any one have an idea, why the commands do not work? Which could I use instead?

GET http://www.domain.com/file.php
wget -q -O /dev/null http://www.domain.com/file.php
php -q /var/www/vhosts/domain.com/httpdocs/shop/file.php

Thanks in advance!

If the command works from the command line (when you are logged in as the user whose cron will run the task) then it will run in cron. Otherwise you will get an error.

So typically you'd use the wget but put it in root's scheduled tasks.

To test it, as root, just run wget http://whatever and then view the content of the file that this generates.
 
Thanks.

But just to confirm, have I used correct settings, to execute the file every minute?

cron.jpg

By the way, in case that this is important for you, I am using only one webspace, for all of my domains.
 
This will execute cron job every hour on the first minute. To run cron every minute, you need to put * * * * *

Put something like, "i'm done, it's working" in that .php file that you're running. Try to access it through web browser - you should get there output, BUT try to run command in shell/terminal (on the server) and if you don't get the same output, something is wrong, in other words cron command is wrong and you'll have to adjust it.

Try to use something like: wget -O - http://www.domain.com/file.php 2> /dev/null
 

wget -h says to "--spider" parameter:
don’t download anything.

Source: http://kau-boys.de/490/webserver/plesk-cronjob-fuer-ein-php-skript-mit-parametern-einrichten

which user you use for cronjob? "root"?

"Tools & Settings >> Scheduled Tasks >> System Users" not included my domain users. I think thats a security risk!? Domain user can create a php thats have access to all! How I can create a cronjob for a specific domain(user)?
 
Last edited:
"Tools & Settings >> Scheduled Tasks >> System Users" not included my domain users. I think thats a security risk!? Domain user can create a php thats have access to all! How I can create a cronjob for a specific domain(user)?

Logging in to Plesk as admin, Tools & Settings >> Scheduled Tasks >> select root from list
 
Logging in to Plesk as admin, Tools & Settings >> Scheduled Tasks >> select root from list
I mean this as a security risk, or not? A webspace user request a cronjob for a script of him and now I add a cronjob with "root" rights?.. his PHP have now root access rights? That can not be right.

If I create a webspace there is a user for this webspace, why I can't set this user as cronjob user? In webmin I can do this.
 
The following command worked without any problems:

wget -q -O /dev/null http://www.domain.com/file.php

The only problem was, the wrong time setting, now when using */1 it executes it every minute (as I wanted).

I run this command as user, not as root. And it works perfect.
 

Running crons with wget is the worst idea i ever saw from a security point of view.

wget is one of the files that needs to have rights 700, else every hackers uploaded script can use it to download the rest of the hack from external websites.

we run our crons like this:

lynx -source http://www.domein.com/cron/cron.php -auth=login:passwd

the advantage is that the php file is executed with the rights of the website user and not root.
the "-auth=login:passwd" is the login info because the cron.php is placed inside a password protected directory.


you will need to set the shell back to /bin/bash or lynx will not be found. But then again that is also true for crons run with wget so that is probably already done.

http://kb.parallels.com/en/115889

This is a list of the files that we give 700 right. You can copy/paste this in a .sh file and run on your server.

#!/bin/sh

chmod 700 /usr/bin/wget
chmod 700 /usr/bin/scp
chmod 700 /usr/bin/who
chmod 700 /usr/bin/w
chmod 700 /usr/bin/locate
chmod 700 /usr/bin/whereis
chmod 700 /sbin/ifconfig
chmod 700 /usr/bin/which
chmod 700 /usr/bin/gcc
chmod 700 /usr/bin/make
chmod 700 /bin/rpm


regards
Jan
 
You have forget to set lynx(and curl) to 700 too, or? ;)

Nope, lynx need to be executable by the user that runs the cron. lynx is a textbrowser and can not "copy" files from another server to your server like wget. An extra advantage of running a cron with lynx is that it is executed with not only the rights, but also the configuration of that website. If the site has a custom php.ini then it will be run with those settings.

curl can do that, but curl is part of php, so 700 or not, if some baddie can upload a php file, he has access to curl.

Interesting pages, most of it i know and apply, some things not. I will investigate them. But imho you can push security to far. Securty has to be a balance between safe and workable and the pantagon needs a different kind of security then the website from the local baker. In the later its usualy mostly a mather of keeping the joomla up to date

regards
Jan
 
Back
Top