Bash script not work fine - linux

I need to write bash script to startup oracle DB
some command should run with user-root and some of them with user-oracle
How can I make these code as a bash script code
#!/bin/bash
su root
password
rm /var/tmp/.oracle/*
su oracle
lsnrctl start
sqlplus sys as sysdba
startup
But after i run this code ,It ask me root password and dont run other commend after su command.
Thanks.

Each line in a script is a command to execute. If a command reads input, it doesn't normally get it from the script file, it gets it from the script's standard input. If you want it to read from the script, you have to use a here-document:
#!/bin/bash
su root <<EOF
password
rm /var/tmp/.oracle/*
su oracle <<EOF1
lsnrctl start
sqlplus sys as sysdba
startup
EOF1
EOF
However, I'm not sure this will work with su. It probably doesn't read the password from standard input, but forces it to come from the terminal. You probably should be using sudo, which you can configure to not require a password for certain users and scripts.
Or if you use systemd, you should be able to configure a service startup script.

Related

Unix function can not called after switch user in shell script

The below script executing with root user.After switch user Unix function showing error.
test.sh
#!/bin/bash
fn_test()
{
echo "This is function"
}
whoami
fn_test
su - oracle<<EOF
whoami
fn_test #This function not called
EOF
exit 0
O/P
root $ ./test.sh
root
This is function
oracle
-ksh[2]: fn_test: not found [No such file or directory]
You have a confusion on what su actually does: you hope it to just swith user when it does start a new process. You can control it with ps in an interactive session: you see the original shell, the su command, the new shell launched by su and the current ps command.
As a shell function is local to the shell it cannot be used in a (grand) child shell.
The best that you can hope is to pass something through the environment. I know that aliases can be put there unsure for functions. Moreover, this is absolutely shell dependant and might not be portable.

Running sudo scripts/bash commands on a remote

I need to remotely start bash scripts that perform sudo tasks, such as chmod and ntpdate and echoing to gpio.
A cron job might be the best solution for some of this, but cron is giving me headaches. I'd like to pass on this venue if I can...
I've confirmed that my scripts work locally (I can ssh into the machine and run them without a hiccup.)
However, If I try to run them remotely like so: (this is within a C++ system call)
ssh user#pc 'bash -s' < /home/user/myScript.sh
Commands with sudo fail.
sudo chmod fails with: no tty present and no askpass program specified
echo to gpio fails with: write error: Device or resource busy
sudo ntpdate fails with: no tty present and no askpass program specified
Can anyone help explain, or help me determine whats happening here?
I'm open to band-aids and different approaches, thanks!
You already found the problem yourself:
sudo chmod fails with: no tty present and no askpass program specified
If you run you shell script via ssh and the script wants to run the command sudo, sudo itself will ask for the users password. But the ssh session is not a tty! How should sudo now prompt for a password and how to get your password?
You can do it if you provide the password in the script ( what makes it very dangerous if someone else can read that script! )
script.sh:
echo "your passwd" | sudo -S
As alternative solution you can run the ssh session with a more privileged user.
ssh privileged_user#pc 'bash -s' < /home/user/myScript.sh
All that comes with some danger. Running all commands from the cript with a more privileged user can also be dangerous!

How to run Cron Job to creates files as User file instead of root file

Why the output file from this is owned by root and not w3svcsadm?
sudo -u w3svcsadm echo "TEST ran" > /home/your/emaildigest/TEST_$( date +%Y%m%d%H%M%S ).output
I'm running into some issues with cron, and I believe this is the key to my problems.
Using the -u flag with sudo executes the command 'echo "TEST ran"' as the user w3svcasadm, but that command isn't the thing doing the work of outputting to a file, which is done by the '>' operator. By the time bash is using that operator, it's already switched back to the user running the shell. If that user is root, then the file will be created under root. In your script, you could use "su w3svcsadm" to switch the shell user before executing that command, then you wouldn't have to use that -u flag at all.

Run a script from root and it calls another script that was in the sudo oracle. how to do that without asking the password of the oracle

I'm logging into a linux machine with root and after login i have used su - oracle to connect my database. Now I've 2 shell scripts one at root home and one at home/oracle. In the home/oracle I've wrote a script for taking the backup of the database. The script available in the root is nohup ssh oracle#onprem-svcdb /home/oracle/test.sh while running the script its asking the password of the oracle, I don't need it to be like this while running the scripts It doesn't need to ask the password and it needs to run the script in oracle. What I need to do for that??? Can anyone help for this
If I understand corrently, you are getting a password prompt on using a script which connects to your database and executes something. If you dont need a password prompt , you would need to generate public and private keys for ssh for the logged in user , in your linux machine and get it configured in the database. Please have a look at the below link
http://docs.oracle.com/cd/E19253-01/816-4557/sshuser-33/index.html
You can try the below
Let this be you env variables.
---------VARIABLES --------------
export APP_USER=something
export APP_PASS=somepass
export APP_SID=sid
Here is the script with a execute permission.
--------------SCRIPT TO RUN SQL----------
#!/usr/ksh
sqlplus << END_OF_SQL
$APP_USER/$APP_PASS#APP_SID
select * from dual;
END_OF_SQL
exit $?
----------END SCRIPT----------
Source : https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:142212348066
you could try expect tool:
You will start expect script below, that will start your main sql-script in return and will send oracle password if prompted:
content of /tmp/expect.exp script:
#!/usr/bin/expect -f
# set Variables
set password '***'
set timeout -1
# what to execute
spawn /usr/bin/su oracle -c "/tmp/main_script.sh"
match_max 100000
# Look for passwod prompt
while {1 > 0 } {
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
}
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
than your main script will be stored in (or root home, whatever dir you need):
/tmp/main_script.sh content:
su - oracle
drop table; create table; other SQL commands
One disadvantage is - plain text password, stored inside the script
How to install: http://www.cyberciti.biz/tips/execute-commands-on-multiple-hosts-using-expect-tool-part-iii.html
Or you could try to modify visudo , like :
user1 ALL=(user2) NOPASSWD: /bin/bash
where : user1 will be granted "su to user2" without password prompt. You can replace also /bin/bash by ALL and then you could launch any command as user2

perl not executing in cron

Ok I'm about to pull my hair out. I have a perl script that just will not run in the crontab however I have a previously written perl script that runs just fine every day on the same box. I have checked all of the given solutions on this site and others around the web and nothing seems to make a difference. Here is my cron and the first part of my script
55 13 * * * su oracle; cd /u02/oraclebackup;./move_em_bkup.pl >> /u02/oraclebackup/move_em_backup.log > move_em_bkup.dbg 2>$1
It touches the .dbg file but does not put anything in there. There are no errors or anything that I can use to go by.
#!/usr/bin/perl
use Strict;
use Archive::Tar;
use Net::SCP qw/ scp /;
use Net::SCP::Expect;
use DateTime;
Can anybody help?
The command you're running is:
su oracle; cd /u02/oraclebackup; ...
su oracle normally launches an interactive shell under the oracle account (assuming you have permission to do so). I'm not sure what that would do in a non-interactive cron environment, but even assuming it works, the cd /u02/oraclebackup and following sub-commands will be executed after that shell terminates, i.e., under the account that owns the crontab. The su oracle will either block the rest of the command or do nothing.
You can use su -c command to run a command as a specified user. In you case, you'd want something like:
su -c oracle sh -c 'cd /u02/oraclebackup; ...'
Or change su to su - if you need the oracle account's login environment.
Better yet, drop the su and put the whole thing in the oracle account's crontab. You might still need to play some more tricks to get the environment right; cron jobs run with a limited set of environment variables by default.

Resources