SSH and exec() users differ - php

I have a Lightsail instance in AWS, and it's a LAMP stack. Previously, I (or bitnami, not sure) created a cron for SSL. It looks something like this:
0 0 * * * sudo /opt/bitnami/letsencrypt/lego --path /o...
If I connect via SSH and run crontab -l, I get the following output (the second cron is for test purposes):
0 0 * * * sudo /opt/bitnami/letsencrypt/lego --path /o...
* * * * * sudo /usr/bin/touch /opt/bitnami/apache2/htdocs/.htaccess
Now, I'm thinking about adding more cron jobs that are related to the app. Adding cron jobs manually (via SSH) is tedious. I want to be able to do this in the UI. I'm saying this because I've seen this done (e.g. in DirectAdmin, Plesk Panel, WHMCS, etc.)
So I started searching for ways to view/edit/delete cron jobs in the PHP. The idea seems simple. Get the current cron jobs (crontabs -l), parse and modify them, and load it back (crontabs file). So I tried to get the current cron jobs (in a PHP file, from the browser), but failed:
exec("crontab -l", $crons);
exec("crontab -u bitnami -l", $crons_bitnami);
exec("sudo crontab -l", $sudo_crons);
exec("sudo crontab -u bitnami -l", $sudo_crons_bitnami);
var_dump(exec("whoami")); // daemon
var_dump(shell_exec("crontab -l")); // NULL
var_dump($crons); // array(0) { }
var_dump($crons_bitnami); // array(0) { }
var_dump($sudo_crons); // array(0) { }
var_dump($sudo_crons_bitnami); // array(0) { }
I get empty results. I get the two cron jobs that I added if I run crontabs -l in the SSH, but this doesn't work in the PHP. So I checked the users. If I type whoami in SSH, I get bitnami. In PHP, it returns daemon. I searched more and figured out that each user has its own cron jobs. In SSH, I'm the user bitnami and it has two cron jobs. Why am I daemon in PHP? Is that the reason I'm getting an empty result? If so, can/how do I change it?
I tested the same code on other servers, and I seem to get correct results. The user is not daemon and I can see the cron jobs. So this might be Lightsail/bitnami related. Nevertheless, is there anything that I can do to fix it?

Related

Running a Cronjob on 1&1 Shared Hosting [Laravel]

In my Laravel application, I have created a scheduled task, as defined in a previous question that I asked. It essentially grabs data and updates a few tables.
I followed a help article on 1&1 about creating cronjobs.
To summarise this article it said the following.
SSH to your server, that you want to set up the new cronjob on, use whereis php to find the location of your PHP Binary, then use the crontab command to access the crontab and add a new job.
This will require the PHP path and the path to the script that is to be run.
The issue is when I run that command I get the following.
No crontab for [username]
So, I then Googled and it seems some shared hosts don't allow you to use or even access the crontab.
On my shared hosting server the path to php-7.1-cli is as follows /usr/bin/php7.1-cli
Now, Artisan is part of Laravel and requires a higher version of PHP than what is found in \usr\bin\php
Given this, for the cronjob I thought I'd be able to do something like this:
* * * * * /usr/bin/php7.1-cli /path/to/artisan schedule:run >> /dev/null 2>&1
Or
* * * * * /usr/bin/php7.1-cli /path/to/mynewable/artisan schedule:run >> /dev/null 2>&1
My folder structure is as follows:
root
/mynewable
But when I run either of these commands, nothing happens.
1&1 also has an inbuilt cron job creator but this only allows me to specify a URL path to a file that would run the necessary script.
Am I being prevented from using crontab? If so, giving a URL seems impractical as the user wouldn't need to validate, but making a path in web routes to run the Laravel scheduler seems a bit of a security hole.
You can create a executable cron.php in your public folder of laravel.
exec("/usr/bin/php7.2-cli -f /homepages/x/xxxxx/htdocs/artisan schedule:run 2>&1", $out, $result);
if(!empty($out)) {
echo implode("<br>\n\n", $out);
}
Now you can call http://example.org/cron.php
Of course you can put the cron.php in a protected directory so no one can call the cron.php.

Execute PHP script in cron job

In our centos6 server. I would like to execute a php script in cron job as apache user but unfortunately it does not work.
Here is the edition of crontab (crontab -uapache -e)
24 17 * * * php /opt/test.php
and here is the source code of "test.php" file which works fine with "apache" user as owner.
<?php exec( 'touch /opt/test/test.txt');?>
I try to replace php with full path of php (/usr/local/php/bin/php) but also it doesn't work.
Automated Tasks: Cron
Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.
You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.
Configuring Cron Tasks
In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:
*/10 * * * * /usr/bin/php /opt/test.php
In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.
* is a wildcard, meaning "every time".
Start with finding out your PHP binary by typing in command line:
whereis php
The output should be something like:
php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz
Specify correctly the full path in your command.
Type the following command to enter cronjob:
crontab -e
To see what you got in crontab.
EDIT 1:
To exit from vim editor without saving just click:
Shift+:
And then type q!
I had the same problem... I had to run it as a user.
00 * * * * root /usr/bin/php /var/virtual/hostname.nz/public_html/cronjob.php
You may need to run the cron job as a user with permissions to execute the PHP script. Try executing the cron job as root, using the command runuser (man runuser). Or create a system crontable and run the PHP script as an authorized user, as #Philip described.
I provide a detailed answer how to use cron in this stackoverflow post.
How to write a cron that will run a script every day at midnight?
I tried all combinations with PATHs, but don't work. Probably they are needed.
In my case, with Centos 7, a reboot or server worked.

call a php page using cron jobs in direct admin

I have a php file that sends an email and it works fine when I open the page with browser. (test.php located in root of my website) But I want the page runs automatically once a day. I found that this is done using cron jobs. I searched a lot and tested a lot of commands and configurations but none of them worked.
I was using * for all time fields assuming that it will run every minute (I didn't like to wait hours to test each configuration)
I tested following commands and many others that I don't remember ):
/usr/bin/php -q http://mysite.com/test.php
/usr/bin/home/php -q http://mysite.com/test.php
/usr/local/bin/php -q http://mysite.com/test.php
/home/myID/php -q http://mysite.com/test.php
#!/usr/local/bin/php -q http://mysite.com/test.php
#!/usr/local/bin/php -q /home/myID/mysite.com/public_html/test.php
Finally, I couldn't figure out what I am doing wrong.
the host is a shared linux host running Direct Admin.
Please tell me a simple step by step guide to set the cron job to run my php file.
thank you
You don't want to call the PHP binary if you're going to run the script via http. You want to use wget (or curl). Example:
*/5 * * * * user /usr/bin/wget http://yoursite.com/cron.html -q -O /dev/null
Where user is the OS user that runs the command.
If you don't have the privileges (shared host) to do something like that, change to use a VPS instance from some provider or use an online cronjob service like https://www.setcronjob.com/.
You could open the page using lynx the command line browser:
lynx -dump http://www.google.com/ > /dev/null
Or use a service like http://cronless.com
Update:
If you setup a cron job in your control panel and it didn't run then most probably your web host disabled it.
My advice is to use a cron job service like cronless.

How to run php jobs regularly in my Amazon EC2 server?

I have a Amazon EC2 server.
And I would like to run php jobs every 1 hours to get the latest information from websites and insert them into database.
But I am not good at server.
Any suggestions?
From this URL, you call crontab from the shell and to execute every hour, add the "00" at the beginning, point to the location of PHP and the script you want to run.
# crontab -e
00 * * * * /usr/local/bin/php /home/dir/myscript.php
As for any Linux server, use crontab.
On the terminal (SSH) :
crontab -e
See the man page for cron

How to set up a cron job via PHP (not CPanel)?

How to set up a cron job via PHP (not CPanel)?
Most Linux systems with crond installed provides a few directories you can set up jobs with:
/etc/cron.d/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
...
The idea here is to create a file in one of these directories. You will need to set the proper permissions/ownership to those (or one of those) directories so that the user launching the PHP script can write to it (Apache user if it's a web script, or whatever CLI user if CLI is used).
The easiest thing is to create an empty file, assign proper permission/ownership to it, and have the PHP script append/modify it.
Per example:
$ touch /etc/cron.d/php-crons
$ chown www-data /etc/cron.d/php-crons
Then in PHP:
$fp = fopen('/etc/cron.d/php-crons', 'a');
fwrite($fp, '* 23 * * * echo foobar'.PHP_EOL);
fclose($fp);
If what you're getting at is dynamically adding lots of jobs to crontab form your application, a better way to do that is manually add ONE cron job:
php -f /path/to/your/runner.php
Store your jobs that you would be adding to cron manually in a table (or one table per task-type), and then have your runner go through the table(s) every minute/hour/day/whatever and execute all the ones that should be executed at that time.
From pure PHP I will create deamon that will manage this (those) cron job(s).
how to create it:
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/ to start with
Finding crontab file isn't easy on shared hosting and there's no certainty that cron will read that file again while it's already running.
Actually I the best way is to use corntab command.
If you don't have access to shell you can use for example PHPShell. Try this.
Uplode a txt file via FTP with jobs in crontab fomat for example
5 * * * * /some/file/to/run.sh > /dev/null
(remember to put a newline at the end of that line)
Log in to your PHPShell and run
crontab uploded_filename.txt
Remember to change file permissions
chmod 775 uploded_filename.txt
Check your cron jobs using
crontab -l
Cheers
There is an embargo on the use of PHP to edit crontabs which has been in place since 2004. You may not be allowed to do this if you live outside of the United States, check with your local government agency.
But seriously, you could always call "crontab -" with a system call. If you need to do this for some user other than the webserver, you'll need some ssh or sudo magic. But it all seems like a bad idea.

Categories