LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Reg sudo - finding user (https://www.linuxquestions.org/questions/linux-newbie-8/reg-sudo-finding-user-4175674722/)

manickaraja 05-06-2020 04:23 PM

Reg sudo - finding user
 
Hi,
I have a requirement to capture current user who runs a program. This program is run by a scheduler agent and that agent is spawned by a specific os user (tidaladm). That user internally run command as another service user (edwadm). Now inside program I want to capture original user (tidaladm). I tried below code snippet bot returns null.

osuser=$(who am i | awk '{print $1}')
osuser=$(ls -l $(tty) | awk '{print $3}')

Both these command return when I manually run it on terminaly. However program run thorough agent is giving this as NULL. Any idea how to get around it?

Thanks,
Manick.

ehartman 05-06-2020 06:41 PM

Quote:

Originally Posted by manickaraja (Post 6120132)
However program run thorough agent is giving this as NULL.

Does the agent run this through a shell (and /bin/bash specific) as you're using shell constructs like $() and | which only work if it's run as a shell script?

manickaraja 05-08-2020 05:40 PM

The command I have pasted is part of a shell script. Agent calls this shell script. Please let me know if it doesnt answer your question.

ehartman 05-08-2020 07:03 PM

Quote:

Originally Posted by manickaraja (Post 6120891)
Agent calls this shell script.

Yeah, but does it try to execute it directly (like one of the exec* functions) or through a shell (like i.e. the system(3) call)?
Quote:

system - execute a shell command
As the name "shell script" already suggests: a shell script has to be executed by a shell, not by any other type of application. Even the hash-bang in the first line is a shell construct: it tells this shell which shell to start as the SUB-shell to really execute the script.

The fork/exec type of calls can only execute pure binaries, not scripts.
There are, of course in Linux, other ways possible, but - as I said - not all of them will handle pure shell constructs in your script.

TB0ne 05-12-2020 08:15 AM

Quote:

Originally Posted by manickaraja (Post 6120132)
Hi,
I have a requirement to capture current user who runs a program. This program is run by a scheduler agent and that agent is spawned by a specific os user (tidaladm). That user internally run command as another service user (edwadm). Now inside program I want to capture original user (tidaladm). I tried below code snippet bot returns null.
Code:

osuser=$(who am i | awk '{print $1}')
osuser=$(ls -l $(tty) | awk '{print $3}')

Both these command return when I manually run it on terminaly. However program run thorough agent is giving this as NULL. Any idea how to get around it?

Your post is confusing...you say you want to capture the user who runs this program. Then you TELL US both users involved; doesn't that mean you already KNOW which user(s) are running this program?

Is there a reason you can't use the names you've already mentioned?? Or show us the rest of the script? What is your actual goal, other than this being a 'requirement'?

manickaraja 05-13-2020 06:40 PM

Quote:

Originally Posted by TB0ne (Post 6122132)
Your post is confusing...you say you want to capture the user who runs this program. Then you TELL US both users involved; doesn't that mean you already KNOW which user(s) are running this program?

Is there a reason you can't use the names you've already mentioned?? Or show us the rest of the script? What is your actual goal, other than this being a 'requirement'?

We have one large script. This script is usually runs as "EDWADM" o/s user.

1) When ran manually by someone by logging into terminal, userx logs into system and do sudo su - edwadm. For this case the command I pasted works perfectly fine and is pulling the O/S user who runs the script (userx in this case).

2) We have a enterprise scheduler where this script is scheduled to run. Scheduler calls the script as local O/S user (edwadm), but scheduler itself run as "root". For this use case I expect osuser to be returned as "root". As root is the user who calls and runs this script as edwadm. However the command I pasted is giving null value.

Hope this clarifies your question.

TB0ne 05-14-2020 07:20 AM

Quote:

Originally Posted by manickaraja (Post 6122657)
We have one large script. This script is usually runs as "EDWADM" o/s user.

1) When ran manually by someone by logging into terminal, userx logs into system and do sudo su - edwadm. For this case the command I pasted works perfectly fine and is pulling the O/S user who runs the script (userx in this case).

2) We have a enterprise scheduler where this script is scheduled to run. Scheduler calls the script as local O/S user (edwadm), but scheduler itself run as "root". For this use case I expect osuser to be returned as "root". As root is the user who calls and runs this script as edwadm. However the command I pasted is giving null value.

Hope this clarifies your question.

And it should also answer yours, since you posted what's wrong. The scheduler is not running as a 'user'...it's running as it's own daemon, and not through a tty. You're NEVER going to get output via the scheduler. Again, you're saying you know the users who are running this script, and it's working manually, which is what you need. Can you not just put a simple check into the script, saying that if you do NOT get a valid osuser, to set it to the scheduler???


All times are GMT -5. The time now is 09:32 AM.