Perl system calls when running as another user using sudo
I think you probably need to add the -i
option ("simulate initial login") to sudo
:
sudo -i -u perluser /usr/bin/perl /data/perlscripts/scripta.pl
That will ensure that .profile
or .login
or whatnot is run properly, and therefore that $PATH
is set up properly and so on. It will really be, in almost all respects, as if perluser
were actually logging in and running /usr/bin/perl /data/perlscripts/scripta.pl
at the shell.
perl: Launch process with system(), then check if its running under a specific display number
I solved it after many attempts.
Did a piped open
$pidofcall = open(HANDLE, "process2|");
Then I did whatever I did need to do, and I made the server send a signal to me if it loses connection with process2. If I did need to bang out, I simply did a "goto killprocess;" Then I simply had:
killprocess:
kill(9,$pidofcall);
close(HANDLE);
$mw->destroy;
system("logout");
Call a perl script from another perl script with specific user authentication
If your program is running as root, you can change the real ($<
) and effective ($>
) UIDs of the process at will. For your purpose, changing EUID is enough:
use v5.12;
say "euid: $>";
system('whoami');
# switch user
$> = 1000;
say "euid: $>";
system('whoami');
# switch back
$>=$<;
say "euid: $>";
system('whoami');
Running that script as root
:
$ sudo perl uidtest
euid: 0
root
euid: 1000
ben
euid: 0
root
How can I check for root when a user runs a perl script
If $>
(aka $EFFECTIVE_USER_ID
if you use English
) is non-zero, then the user is not root.
how can I run a perl script which needs root?
Usually you would tell sudo that the webserver user can execute the script without need to authenticate with a password. You'll have to add a line like the following in /etc/sudoers
:
www-data ALL=(ALL:ALL) NOPASSWD: /root/scripts/script.pl
This would let the www-data user execute script.pl
via sudo without having to enter a password.
Then execute the script from PHP using
exec('sudo /root/scripts/script.pl');
Related Topics
Is It Correct to Use The Rc.Local File to Start a Program When The System Starts
Pg_Dump Not Been Killed Inside Script with Kill Command
How to Get The User's Domain Information in Samba
Kernel Module Build Fails: Sys/Types.H: No Such File or Directory
Function Return Values Within Bash If Statements
Howto Use Sed to Remove Only Triple Empty Lines
How to Get Wget Only Save File If Its Complete
How to Grep for Presence of Specific Hex Bytes in Files
Remove Strings by a Specific Delimiter
Is There a Linker Flag to Force It to Load All Shared Libraries at Start Time
Does a Fully Qualified Domain Name Need a Period
Cython Standalone Executable on Ubuntu
Cvs Tab Completion for Modules Under Linux
Which Signal Was Delivered to Process Deadlocked in Signal Handler