Change to user root without end the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change to user root without end the script
# 1  
Old 01-13-2010
Change to user root without end the script

Hello there!
I need help:

I have this script:
Code:
#!/bin/bash

#chage to root user
sudo su

#Insert actual date
echo -n "Ingrese fecha actual: "

#Read the actual date
read fecha

#clean RAM memory
sync ;  echo 3 > /proc/sys/vm/drop_caches ; swapoff -a && swapon -a

#backup opennms files
tar cvfzP opennms_etc_$fecha.tar.gz /etc/opennms/* -R

#copy PostgreSQL Client Authentication Configuration File
cp /etc/postgresql/8.3/main/pg_hba.conf pg_hba.conf

#copy PostgreSQL configurarion file
cp /etc/postgresql/8.3/main/postgresql.conf postgresql.conf

#change to postgres user
su - postgres

#backup PostgreSQL database
pg_dumpall  > opennms_all_$fecha.sql
exit

#Move PostgreSQL database to the home directory
sudo mv opennms_all_$fecha.sql /home/bobbasystem

#Change to default user
su - bobbasystem[/B]

[B]#copy all backuped files to main server
scp opennms_etc_$fecha.tar.gz trancos@172.17.40.232:
scp opennms_all_$fecha.sql     trancos@172.17.40.232:
scp pg_hba.conf               trancos@172.17.40.232:
scp postgresql.conf           trancos@172.17.40.232:

#end

and when I change in the first line to root user, the script terminates without executing the rest.
If I delete these instructions and run the script with sudo su,
the instructions su -postgres can be executed, but not the rest.

How do I get the script does not end in these instructions?

Sorry for my english.

Thanks!

Last edited by bobbasystem; 01-13-2010 at 09:06 AM.. Reason: Please use code tags
# 2  
Old 01-13-2010
The reason is simple: su runs a program given on the command line as the user specified, or just executes a shell if none is given. It won't change the permissions of a script it's called from (can't even, as that would have to be done by the shell itself).

If you have it, you can use sudo with the -u switch to change the user for single commands. Otherwise, you're probably better off if you put the commands into a temporary script, run that as root/postgres, and delete it afterwards.
# 3  
Old 01-13-2010
thanks for your help pludi!

Quote:
Originally Posted by pludi
The reason is simple: su runs a program given on the command line as the user specified, or just executes a shell if none is given. It won't change the permissions of a script it's called from (can't even, as that would have to be done by the shell itself).

If you have it, you can use sudo with the -u switch to change the user for single commands. Otherwise, you're probably better off if you put the commands into a temporary script, run that as root/postgres, and delete it afterwards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Switch from Local user to root user from a shell script?

Hi, I need to switch from local user to root user in a shell script. I need to make it automated so that it doesn't prompt for the root password. I heard the su command will do that work but it prompt for the password. and also can someone tell me whether su command spawns a new shell or... (1 Reply)
Discussion started by: Little
1 Replies

2. UNIX for Dummies Questions & Answers

Unable to change password using root user

Hi, I tired changing password for mqm user in linux server with root user. But still I couldn't able to login mqm user with changed password. Can anyone please help on this. # passwd mqm Thanks, Anusha (4 Replies)
Discussion started by: Anusha M
4 Replies

3. Shell Programming and Scripting

How to Login as another user through Shell script from current user[Not Root]

Hi Every body, I would need a shell script program to login as different user and perform some copy commands in the script. example: Supppose ora_toms is the active user ora_toms should be able to run a script where user: ftptomsp pass: XXX should login through and run the commands ... (9 Replies)
Discussion started by: ujjwal27
9 Replies

4. UNIX for Dummies Questions & Answers

Sudo to delegate permission from non-root user to another non-root user

I've been through many threads before i decide to create a separate thread. I can't really find the solution to my (simple) problem. Here's what I'm trying to achieve: As "canar" user I want to run a command, let's say "/opt/ocaml/bin/ocaml" as "duck" user. The only to achieve this is to... (1 Reply)
Discussion started by: canar
1 Replies

5. Shell Programming and Scripting

Need to run a bash script that logs on as a non-root user and runs script as root

So I have a script that runs as a non-root user, lets say the username is 'xymon' . This script needs to log on to a remote system as a non-root user also and call up a bash script that runs another bash script as root. in short: user xymon on system A needs to run a file as root user and have... (2 Replies)
Discussion started by: damang111
2 Replies

6. Shell Programming and Scripting

Create script for change root password via SSH

HI I 'm new shall script and unix. I want to create script for change password root by ssh-keygen command . I have 50 servers and I want ot login ot the servers via ssh by type ones password and can login every machines.The script ssh-keygen must ot generate key every weekly than it send new... (2 Replies)
Discussion started by: pellnapook
2 Replies

7. Shell Programming and Scripting

root user command in shell script execute as normal user

Hi All I have written one shell script for GPRS route add is given below named GPRSRouteSet.sh URL="www.google.com" VBURL="10.5.2.211" echo "Setting route for $URL for GPRS" URL_Address=`nslookup $URL|grep Address:|grep -v "#"|awk -F " " '{print $2}'|head -1` echo "Executing ... (3 Replies)
Discussion started by: mnmonu
3 Replies

8. Solaris

cant able to change permission in a DIR as root user

Hi my directory not accepting any commands. its simply telling permission denied. i tried ( cp, mv, rm ) as roor i want to set default permissons to this DIR please find the Logs below. dr-xr-xr-x 1 root root 1 Jun 1 09:04 AP1_ROP ( original dir) root> chmod 777... (5 Replies)
Discussion started by: vijayq8
5 Replies

9. Shell Programming and Scripting

How do i change to super user then revert back to ordinary user ,using shell script?

Hi all, I am trying to eject the cdrom from a livecd after certain stage... Now assuming that it is possible to eject,please consider my issue!!! The OS boots into a regular user by default...so i am unable to use the eject command to push out the drive... However if i try pfexec eject it... (3 Replies)
Discussion started by: wrapster
3 Replies

10. Shell Programming and Scripting

script to change root password

Hi All, I'm looking for a script to change root password for about 200 servers of both Solaris and Linux. I'm not very good at scripting, can anyone share a script if you already have one? :) (3 Replies)
Discussion started by: kvadivel80
3 Replies
Login or Register to Ask a Question