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?