Python Forum
Linux Script Sudo and "&" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Linux Script Sudo and "&" (/thread-20737.html)

Pages: 1 2


Linux Script Sudo and "&" - s3fac5 - Aug-28-2019

Hello everyone in the forums of python
i am new to language
and i have make a small script
which i want to run
by using the followed method from linux terminal

sudo python myscript.py &


the problem is that the operating system creates a process ID
but doesnt show the GUI when i do fg command its shows up

also its shows when i remove the "&" char at then end

the script contains a GUI which had been made by a Designer and a code to it
from the autogenerated command pyuic
the rest of the commands is hand-written

any help is appreciate it

thanks everyone


RE: Linux Script Sudo and "&" - Axel_Erfurt - Aug-28-2019

what is the "&" in the end?

If you want to pass a parameter to the script you can try:

if __name__ == '__main__':
    ......
    if len(sys.argv) > 1:
        myvalue = sys.argv[1]
        .....
sudo python myscript.py parameter


RE: Linux Script Sudo and "&" - s3fac5 - Aug-28-2019

(Aug-28-2019, 07:25 AM)Axel_Erfurt Wrote: what is the "&" in the end?

If you want to pass a parameter to the script you can try:

if __name__ == '__main__':
    ......
    if len(sys.argv) > 1:
        myvalue = sys.argv[1]
        .....
sudo python myscript.py parameter

the "&" character under Linux operating system is a special one
its gives the ability to "disengange" the process from the terminal
and make the program to run autonomous from the terminal

eg i open the terminal and i want to open firefox

with the command "firefox &" i can close the terminal and the firefox will be up

without the "&" at the end when i close the terminal the firefox will close

same situation with almost every python script
so i am losing something up cause my newbie state
thanks


RE: Linux Script Sudo and "&" - metulburr - Aug-28-2019

Add a shebang line on the top of the script to the path of your intended python interpreter
#!/usr/bin/python3.6
make the script executable
sudo chmod +x myscript.py
and then try the command again
sudo python myscript.py &
On mine i was getting the same issue. It stopped when sudo and & were present in the command.
Output:
metulburr@ubuntu:~$ sudo python3.6 test11.py & [3] 16587 [2]+ Stopped sudo python3.6 test11.py metulburr@ubuntu:~$
but worked after making those same changes
Output:
metulburr@ubuntu:~$ sudo python3.6 test11.py & [4] 17172 metulburr@ubuntu:~$



RE: Linux Script Sudo and "&" - s3fac5 - Aug-28-2019

(Aug-28-2019, 08:32 AM)metulburr Wrote: Add a shebang line on the top of the script to the path of your intended python interpreter
#!/usr/bin/python3.6
make the script executable
sudo chmod +x myscript.py
and then try the command again
sudo python myscript.py &
On mine i was getting the same issue. It stopped when sudo and & were present in the command.
Output:
metulburr@ubuntu:~$ sudo python3.6 test11.py & [3] 16587 [2]+ Stopped sudo python3.6 test11.py metulburr@ubuntu:~$
but worked after making those same changes
Output:
metulburr@ubuntu:~$ sudo python3.6 test11.py & [4] 17172 metulburr@ubuntu:~$

its keeps to do the same
its suppose to
A)To ask the sudo password
B)To open the GUI


RE: Linux Script Sudo and "&" - DeaD_EyE - Aug-28-2019

Why do you run your program as root?
Can you start it as normal user?

Sudo runs your programs in a different environment. If you have an activated virtuelenv, then it would be not used with sudo.
If you need access to resources like ttyUSB0, you can add the dialup group (is in many distributions the case) and after a new login, the user has the right to access ttyURB0. If you want to open a port below 1024, it's not possible without root access.


RE: Linux Script Sudo and "&" - s3fac5 - Aug-28-2019

(Aug-28-2019, 10:15 AM)DeaD_EyE Wrote: Why do you run your program as root?
Can you start it as normal user?

Sudo runs your programs in a different environment. If you have an activated virtuelenv, then it would be not used with sudo.
If you need access to resources like ttyUSB0, you can add the dialup group (is in many distributions the case) and after a new login, the user has the right to access ttyURB0. If you want to open a port below 1024, it's not possible without root access.

hello i want to run a command with sudo
because from the nature it cant be run as normal user
in fact i have make a subprocess with Popen and PIPES
so this is the problem
i need sudo cause the internal command in subprocess needs sudo
thanks again


RE: Linux Script Sudo and "&" - Axel_Erfurt - Aug-28-2019

If it is a GUI Application you should not use sudo. Use gksu then you will be asked for the password.


RE: Linux Script Sudo and "&" - s3fac5 - Aug-28-2019

(Aug-28-2019, 04:18 PM)Axel_Erfurt Wrote: If it is a GUI Application you should not use sudo. Use gksu then you will be asked for the password.

having the python exec command at the beggining of the file as mentioned before
and trying to running via pkexec command
i get no GUI

in fact i noticed the same behaviour

the pkexec dialog popup shows up i enter my password
and then boom
nothing happens the hdd do some job
but nothing

also i noticed the same behavior not only with

"pkexec script.py"

but with

"pkexec script.py &"

so its something with the way the unix system handles the python script
and vise versa

thanks


RE: Linux Script Sudo and "&" - Axel_Erfurt - Aug-28-2019

if the script is just for you, try that
Quote:echo <password> | sudo -S <command>