Reputation: 1045
I have a python script that runs on a loop. It will run from a certain user. I only want it to execute the code when a certain user is using the computer. However, if I do switch user, then the python script still runs.
Example:
while 1:
if user == "kim":
#do something
pass
Edit:
The biggest problem with my problem is that the script is still running in my user's account. Even if I switch accounts, my account is still logged in and my account still owns the process. I want to know how to get the currently active user. This is not the same user as the user the Python script is running from. The suggested answers and comments only give me the user from which Python is running from. I am using Yosemite (OS X) and built-in modules are fine, but downloading should be avoided.
Is there a portable way to get the current username in Python? does not solve my problem as it returns the same username even when I switch users.
Upvotes: 2
Views: 1561
Reputation: 520
Use the getpass
library to get the username.
import getpass
user = getpass.getuser()
while 1:
if user == "kim":
#do something
pass
Upvotes: 2