Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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.
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'?
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.
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???
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.