Identify if ran by su or sudo?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify if ran by su or sudo?
# 1  
Old 05-28-2010
Java Identify if ran by su or sudo?

Recently I was on an operational call and heard the people running my code placing the code in the /tmp directory and running as root. I had not planned on that. So I want to add some checks to my code (using ksh93):

# ---------- ---------- ----------
# root not allowed to run this
# ---------- ---------- ----------
[[ $( whoami ) = root ]] && exit 2

# ---------- ---------- ----------
# Don't run this in /tmp
# ---------- ---------- ----------
[[ $( pwd ) = /tmp* ]] && exit 3

# ---------- ---------- ----------
# Don't run as sudo
# ---------- ---------- ----------

# ---------- ---------- ----------
# How much space is available here?
# Filesystem    Type   1K-blocks      Used Available Use% Mounted on
# /dev/sda3     ext3    68588072  32259392  32788356  50% /home
# ---------- ---------- ----------
integer _avail=$( df -k . | grep -v "^Filesystem" | awk '{ print $4 }' )
integer _threshold=26214400  # 25GB
(( _avail < _threshold )) && {

    print -u2 "***> There is not enough space (25gb) on this mount point!"
    print -u2 "     Only found ${_avail} kb."
    exit 4


What I can't find is a means to check the case where my code was run with sudo or "su -". The above 'whomai' returns the users name, not the fact it was run with root access through sudo. There are many reasons to use sudo, but in this case I want my app to run as a normal user with normal permissions, no extra help.

Is this possible? any ideas? I've done some google searching and all the references I can find are on how to use sudo, not how to identify it is being used.

Thanks for any help.
# 2  
Old 05-28-2010
ant:/home/vbe $ whoami
ant:/home/vbe $ who am i
vbe        pts/2        May 19 10:25

# 3  
Old 05-28-2010


>sudo echo $( whoami )

How do I know that sudo was being used?
# 4  
Old 05-28-2010
$ echo $(id -u)
$ sudo echo $(id -u)

[ $(id -u) -eq 0 ] && exit 2
# or
$(($(id -u))) || exit 2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How many times did the scipt ran?

Hi, Can we able to identify how many times any script was ran per day/week/month/year wise (2 Replies)
Discussion started by: penqueen
2 Replies

2. Shell Programming and Scripting

Checking if command ran in parallel

Hi, I am running below code For I in $var do .......some line of code....... nohup Sqlplus apps/apps <<EOF & My_proc($I) Exit EOF done Nohup and & is used for parallel processing. Can someone help in determining if the procedure with different arguments Was called paralley or... (3 Replies)
Discussion started by: Pratiksha Mehra
3 Replies

3. UNIX for Dummies Questions & Answers

Test if script was ran w/ nohup

I want to test if is being run with nohup ... but $0 does not contain nohup part... purpose: should only be ran with nohup if user forgets nohup then it should echo "run with nohup" && exit....... (2 Replies)
Discussion started by: holyearth
2 Replies

4. Shell Programming and Scripting

ssh sudo command - Prompts for sudo password as visible text. Help?

I am writing a BASH script to update a webserver and then restart Apache. It looks basically like this: #!/bin/bash rsync /path/on/local/machine/ ssh sudo /etc/init.d/apache2 reloadrsync and ssh don't prompt for a password, because I have DSA encryption... (9 Replies)
Discussion started by: fluoborate
9 Replies

5. UNIX for Advanced & Expert Users

How to identify the scripts ran at a particular day of last month?

How to identify the scripts ran at a particular day of last month? I have to identify a script that ran on 06/01/2011 @ 4 am (3 Replies)
Discussion started by: rdhanek
3 Replies

6. Shell Programming and Scripting

how to find whether a script ran or not

Hi, I have written a script and placed in an application and the script can be executed manually only. But somehow one of the method in the script is being called and bringing the application down. But we are not able to find any instance of script running. Is there a way to findout whether the... (1 Reply)
Discussion started by: Satyak
1 Replies

7. AIX

IBM AIX ran into error u0.1-p1-v2

Hi Guys, Is there any one can help me with the below error message dispalys on the LED panel. 10112633 u0.1-p1-v2 I bought a second hand IBM 7029 6C3 online several days ago. On the fist day, it can be ran well. but on second day, when i tried to boot up the machine, after few mins, the... (2 Replies)
Discussion started by: andylai
2 Replies

8. UNIX for Dummies Questions & Answers

Find hangs when ran under superuser.

When I ran the following find command under a "regular" user is completes but it limited because of perms. find / -name "*.*" | xargs grep something > ok But when I try to run it under su, it hangs and never completes. Any suggestion? (4 Replies)
Discussion started by: shorty
4 Replies

9. HP-UX

Aaarrggghhh HP warranty ran out

We have HP 9000 running UX. Does anybody know a reputable or even a strong maintenance firm that DOESN'T have a help desk the other side of the planet? We've been approached by Newcorp but thay seem rubbish. Please give me some recommendations. Sorry if this is posted in the wrong room but I... (3 Replies)
Discussion started by: Handsy
3 Replies

10. Shell Programming and Scripting

which user ran which command

can we come to know all the command ran bya user for last 1 day (1 Reply)
Discussion started by: narang.mohit
1 Replies
Login or Register to Ask a Question

Featured Tech Videos