Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 308 characters in body
Source Link

I want to execute a command line command as root from a php file. In my case I want to view the crontab file in my php page.

Running the sudo command in php is not recommended and on my raspberry pi with raspberry pi OS the php line

echo (shell_exec("sudo more /var/spool/cron/crontabs/pi"));

does not work. So I created a shell script:

crontab.sh

#!/bin/bash
echo "Start Crontab"
sudo more /var/spool/cron/crontabs/pi
echo "End Crontab"

and I created a php page: crontab.php

<?php
echo "Get Current User: " . get_current_user();
echo "<br>Who am I: ".(exec("whoami"));
echo "<br>";
echo (shell_exec("/var/www/html/cmd/crontab.sh"));
?>

When I execute the crontab.sh in the command line. It seems to work. Because I see the crontab text. When I load the php page. I do not see the crontab text. I only see

Get Current User: pi
Who am I: www-data
Start Crontab End Crontab

What can I do? I had a cron that used rsync to copy the crontab -e file to a cron.txt file every hour. It worked but i do not want to view an (old) copy.

edit: My problem is that the task that starts with sudo gives zero output. In the comments I got the suggestion to use sudo crontab -l. That's better than the line I used because it gives the root crontab and I just did not know of the -l solution. But the problem is still there. There is zero output.

I want to execute a command line command as root from a php file. In my case I want to view the crontab file in my php page.

Running the sudo command in php is not recommended and on my raspberry pi with raspberry pi OS the php line

echo (shell_exec("sudo more /var/spool/cron/crontabs/pi"));

does not work. So I created a shell script:

crontab.sh

#!/bin/bash
echo "Start Crontab"
sudo more /var/spool/cron/crontabs/pi
echo "End Crontab"

and I created a php page: crontab.php

<?php
echo "Get Current User: " . get_current_user();
echo "<br>Who am I: ".(exec("whoami"));
echo "<br>";
echo (shell_exec("/var/www/html/cmd/crontab.sh"));
?>

When I execute the crontab.sh in the command line. It seems to work. Because I see the crontab text. When I load the php page. I do not see the crontab text. I only see

Get Current User: pi
Who am I: www-data
Start Crontab End Crontab

What can I do? I had a cron that used rsync to copy the crontab -e file to a cron.txt file every hour. It worked but i do not want to view an (old) copy.

I want to execute a command line command as root from a php file. In my case I want to view the crontab file in my php page.

Running the sudo command in php is not recommended and on my raspberry pi with raspberry pi OS the php line

echo (shell_exec("sudo more /var/spool/cron/crontabs/pi"));

does not work. So I created a shell script:

crontab.sh

#!/bin/bash
echo "Start Crontab"
sudo more /var/spool/cron/crontabs/pi
echo "End Crontab"

and I created a php page: crontab.php

<?php
echo "Get Current User: " . get_current_user();
echo "<br>Who am I: ".(exec("whoami"));
echo "<br>";
echo (shell_exec("/var/www/html/cmd/crontab.sh"));
?>

When I execute the crontab.sh in the command line. It seems to work. Because I see the crontab text. When I load the php page. I do not see the crontab text. I only see

Get Current User: pi
Who am I: www-data
Start Crontab End Crontab

What can I do? I had a cron that used rsync to copy the crontab -e file to a cron.txt file every hour. It worked but i do not want to view an (old) copy.

edit: My problem is that the task that starts with sudo gives zero output. In the comments I got the suggestion to use sudo crontab -l. That's better than the line I used because it gives the root crontab and I just did not know of the -l solution. But the problem is still there. There is zero output.

added 17 characters in body
Source Link
Chris Davies
  • 128.2k
  • 16
  • 179
  • 324

I want to execute a command line command as root from a php file. In my case I want to view the crontab file in my php page.

Running the sudosudo command in php is not recommended and on my raspberry pi with raspberry pi OS the php line: echo (shell_exec("sudo more /var/spool/cron/crontabs/pi"));

echo (shell_exec("sudo more /var/spool/cron/crontabs/pi"));

does not work. So I created a shell script:

crontab.sh

#!/bin/bash
echo "Start Crontab"
sudo more /var/spool/cron/crontabs/pi
echo "End Crontab"

and I created a php page: crontab.php

<?php
echo "Get Current User: " . get_current_user();
echo "<br>Who am I: ".(exec("whoami"));
echo "<br>";
echo (shell_exec("/var/www/html/cmd/crontab.sh"));
?>

When I execute the crontab.sh in the command line. It seems to work. Because I see the crontab text. When I load the php page. I do not see the crontab text. I only see

Get Current User: pi
Who am I: www-data
Start Crontab End Crontab

What can I do? I had a cron that used rsyncrsync to copy the crontab -ecrontab -e file to a cron.txtcron.txt file every hour. It worked but i do not want to view an (old) copy.

I want to execute a command line command as root from a php file. In my case I want to view the crontab file in my php page.

Running the sudo command in php is not recommended and on my raspberry pi with raspberry pi OS the php line: echo (shell_exec("sudo more /var/spool/cron/crontabs/pi")); does not work. So I created a shell script:

crontab.sh

#!/bin/bash
echo "Start Crontab"
sudo more /var/spool/cron/crontabs/pi
echo "End Crontab"

and I created a php page: crontab.php

<?php
echo "Get Current User: " . get_current_user();
echo "<br>Who am I: ".(exec("whoami"));
echo "<br>";
echo (shell_exec("/var/www/html/cmd/crontab.sh"));
?>

When I execute the crontab.sh in the command line. It seems to work. Because I see the crontab text. When I load the php page. I do not see the crontab text. I only see

Get Current User: pi
Who am I: www-data
Start Crontab End Crontab

What can I do? I had a cron that used rsync to copy the crontab -e file to a cron.txt file every hour. It worked but i do not want to view an (old) copy.

I want to execute a command line command as root from a php file. In my case I want to view the crontab file in my php page.

Running the sudo command in php is not recommended and on my raspberry pi with raspberry pi OS the php line

echo (shell_exec("sudo more /var/spool/cron/crontabs/pi"));

does not work. So I created a shell script:

crontab.sh

#!/bin/bash
echo "Start Crontab"
sudo more /var/spool/cron/crontabs/pi
echo "End Crontab"

and I created a php page: crontab.php

<?php
echo "Get Current User: " . get_current_user();
echo "<br>Who am I: ".(exec("whoami"));
echo "<br>";
echo (shell_exec("/var/www/html/cmd/crontab.sh"));
?>

When I execute the crontab.sh in the command line. It seems to work. Because I see the crontab text. When I load the php page. I do not see the crontab text. I only see

Get Current User: pi
Who am I: www-data
Start Crontab End Crontab

What can I do? I had a cron that used rsync to copy the crontab -e file to a cron.txt file every hour. It worked but i do not want to view an (old) copy.

Source Link

Execute an shell script with sudo inside from php

I want to execute a command line command as root from a php file. In my case I want to view the crontab file in my php page.

Running the sudo command in php is not recommended and on my raspberry pi with raspberry pi OS the php line: echo (shell_exec("sudo more /var/spool/cron/crontabs/pi")); does not work. So I created a shell script:

crontab.sh

#!/bin/bash
echo "Start Crontab"
sudo more /var/spool/cron/crontabs/pi
echo "End Crontab"

and I created a php page: crontab.php

<?php
echo "Get Current User: " . get_current_user();
echo "<br>Who am I: ".(exec("whoami"));
echo "<br>";
echo (shell_exec("/var/www/html/cmd/crontab.sh"));
?>

When I execute the crontab.sh in the command line. It seems to work. Because I see the crontab text. When I load the php page. I do not see the crontab text. I only see

Get Current User: pi
Who am I: www-data
Start Crontab End Crontab

What can I do? I had a cron that used rsync to copy the crontab -e file to a cron.txt file every hour. It worked but i do not want to view an (old) copy.