LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-06-2020, 04:23 PM   #1
manickaraja
Member
 
Registered: Sep 2003
Location: India
Posts: 36

Rep: Reputation: 0
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.
 
Old 05-06-2020, 06:41 PM   #2
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by manickaraja View Post
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?
 
Old 05-08-2020, 05:40 PM   #3
manickaraja
Member
 
Registered: Sep 2003
Location: India
Posts: 36

Original Poster
Rep: Reputation: 0
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.
 
Old 05-08-2020, 07:03 PM   #4
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by manickaraja View Post
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.
 
1 members found this post helpful.
Old 05-12-2020, 08:15 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,689

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by manickaraja View Post
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'?
 
1 members found this post helpful.
Old 05-13-2020, 06:40 PM   #6
manickaraja
Member
 
Registered: Sep 2003
Location: India
Posts: 36

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
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.
 
Old 05-14-2020, 07:20 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,689

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by manickaraja View Post
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???
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reg: Reg Single Process Multiple sockets! bkannadassan Linux - Networking 3 07-19-2018 02:37 PM
LXer: The Ultimate Sudo FAQ — To Sudo Or Not To Sudo? LXer Syndicated Linux News 13 04-13-2013 01:36 AM
Windows: Which program can track the reg updates at software install and make a reg? frenchn00b General 5 11-20-2008 04:26 PM
finding an offset directory with reg expression airswit Programming 1 02-15-2006 12:52 AM
Nice surprise Kaffeine plays Reg 1 and Reg 2 disks 1kyle SUSE / openSUSE 1 10-10-2005 04:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration