LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-09-2008, 03:22 AM   #1
muazfarooqaslam
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Rep: Reputation: 15
Running Scripts on Login


Hi Guys,

I have a situation where it is required that a scripts should automatically run when they login for like 10 specific users all having different shells as default. Some are using Ksh and some are using bash. I have done some homework on it but still confused with following options,

1. Add entry in /etc/profile that if id != 0 run this script.

2. Add entry in ~/.bashrc of all users.

3. Add entry in ~/.bash_profile of all users. (But with this option script wont run if shell is non-login for e.g. if i do a su to any user from root)

Forgot to mention that all users have following files in their home directories,

a) .profile
b) .bashrc
c) .bash_profile

Even though some users have Ksh as default, there is no Ksh related file in their home directories.

Please help me in making right choice. Incase i have left any option, please mention that also.

thanks
 
Old 01-09-2008, 04:07 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
The one resource these shells have in common is /etc/profile.

Quote:
Originally Posted by muazfarooqaslam View Post
Add entry in /etc/profile that if id != 0 run this script
Wrt system users IMHO this should really be [ $ID -ge $UID_MIN ] (/etc/login_defs).

BTW, here's how to find the difference between Ksh and BASH (taken from Rootkit Hunter):
Code:
if print >/dev/null 2>&1; then echo ksh; else echo bash; fi
 
Old 01-09-2008, 04:36 AM   #3
muazfarooqaslam
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Original Poster
Rep: Reputation: 15
so you recommend adding entry in /etc/profile?

BTW the users for which i want to run this script have UIDs from 16004 -16018. So the test can be like this

[ $ID -ge 16004 -a $ID -le 16018 ]

What is (/etc/login_defs) file for?

If i add entry in /etc/profile, will the script run for non-login shells also?
 
Old 01-09-2008, 06:47 AM   #4
muazfarooqaslam
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Guys i am having hard time figuring out the solution. The method proposed by unSpawn will not work as /etc/profile will not be checked for nonlogin shells and consequently script wont run. Let me explain the scenario again,

SCENARIO: i want a script to be executed each time some specific users login either through login shell or non-login shell by doing su from root etc.

Should i add an entry in ~/.bashrc of each user? This way script will run irrespective of the shell being login or nonlogin as bashrc is sourced in bash_profile which is called for login shells.

Please can someone confirm that this is the proper way of doing it.
 
Old 01-09-2008, 03:16 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
Then do something with like pam_script or so. Anything in ~/ can and will be modded by users (unless you chattr it but that kinda defeats having personal resource files) leaving you w/o control, besides in Ksh it's ~/.profile.
 
Old 01-12-2008, 02:53 AM   #6
muazfarooqaslam
LQ Newbie
 
Registered: Dec 2007
Posts: 22

Original Poster
Rep: Reputation: 15
I have achieved the goal but in an unusual way.

Target: The original target was to restrict usres to do all kind of stuff after loging in. They only should be able to execute some specific commands.

Solution:

1. Created following script and placed it in /usr/bin,

Code:
command=11
while [ "$command" != "Q" -o "$command" != "q" ]
do
  echo -e "\033[32m**************************************\033[0m"
  echo -e "\033[32mEnter your command (1, 2, 3, 4, 5 or Q)\033[0m"
  echo -e "\033[32m**************************************\033[0m"
  echo -e "\033[36mdf -k                                : 1\033[0m"
  echo -e "\033[36msar 1 5                              : 2\033[0m"
  echo -e "\033[36muptime                               : 3\033[0m"
  echo -e "\033[36mfree mem                     : 4\033[0m"
  echo -e "\033[36mPostpaid MMS Count           : 5\033[0m"
  echo -e "\033[36mQuit                         : Q or q\033[0m"
  echo -e "\033[32m**************************************\033[0m"
  echo -n "Your Selection: "
  read command
  echo " "
  case "$command" in
          1)
                  df -k
                  echo " "
                  ;;
          2)
                  sar 1 5
                  echo " "
                  ;;
          3)
                  uptime
                  echo " "
                  ;;
          4)
                  free mem
                  echo " "
                  ;;

          5)
                  echo -n "Please enter date: "
                  read target
                  /usr/bin/trstat.x $target 2>> /dev/null
                  echo " "
                  ;;

          Q)
                  exit
                  ;;
          q)
                  exit
                  ;;
          *)
                  echo -e "\033[31mBad command, your choices are: 1, 2, 3, 4, 5 or Q/q\033[0m"
                  echo
                  ;;
  esac
done
2. Created sort of another shell in /bin directory. Following is the content of shell file,

Code:
node1# cat ressh
#!/bin/bash
/bin/bash /usr/bin/login_script
node1#
3. For all users to be restricted, i modified /etc/passwd and set their default shell to ressh.

Now when any restricted user logs in to the system, the above script is automatically executed and he have no choice but to select from the available command. Even if wants to hit Ctrl+C to exit from script, he instead will logout from the system.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
samba login scripts not running dholland Linux - Networking 0 11-20-2007 11:38 AM
Login Scripts??? Help Orian Linux - Newbie 3 11-12-2005 02:29 PM
Login Scripts DarkNecromancer Linux - General 4 05-11-2005 10:26 AM
Running Scripts at User Login? Setheck Linux - Software 6 09-27-2004 11:24 AM
Need help running scripts from scripts sdouble Linux - Newbie 3 05-31-2004 01:56 PM


All times are GMT -5. The time now is 01:01 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration