sh - Cron - Environment Variables - cron

I'm trying use a System Script to verify the status of server, in past I implement something like check PID of process is running or not, but I think this is the best way, but I'm stucked to put this running on a Cron. I'll try to resume this as possible.
My Script
/usr/IBM/WebSphere/AppServer/profiles/bpm/bin/serverStatus.sh $server -username adm -password adm
System Script - serverStatus.sh
binDir=`dirname ${0}`
. ${binDir}/setupCmdLine.sh # Here he set the env variables
${WAS_HOME}/bin/serverStatus.sh "$#" # Here he call the global script with
# all env variables set to use the script
I don't want to change anything related to PATH because some variables of others scripts depends of the current path to use the WAS_HOME, anyway to solve this and put this on Cron.
thanks in advance
tiago

Solve the problem using:
/usr/IBM/WebSphere/AppServer/bin/serverStatus.sh -all -username adm -password adm

Related

How to connect to SQLPLUS and run SQL script within ssh unix?

I have the following bash script:
#!/bin/bash
set command "sqlplus -s user/password#tns"
ssh -t test#192.168.94.139 $command
Now, I want to run te following sql script (which is on the other device I'm accessing):
/usr/mikael/myfile.sql
How is the best practice to run the script from that path?
I've seen a suggestion to add "/usr/mikael/myfile.sql" to the end of the command, as :
set command "sqlplus -s user/password#tns /usr/mikael/myfile.sql"
Is that really good? (I'm working on a prod environment and don't want to mess with it).
Thanks.
set does not do what you think it does. Use
command="sqlplus -s user/password#tns #/usr/mikael/myfile.sql"

How to pipe postfix input to a specific script with user and path?

How can I pipe emails for a specific user to a script AND that script to be ran with a specific user's login.
I have this in my /etc/postfix/master.cf file:
my_transport unix - n n - 50 pipe
flags=R user=deploy argv=/srv/www/myscript
My script prints $PATH and whoami to a file and this is what I get
PATH=/usr/bin:/bin
whoami = deploy
If I run sudo su - deploy and run echo "PATH=$PATH" and echo "whoami = $(whoami)" I get
PATH=/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
whoami = deploy
How do I make postfix run myscript as deploy user but with it's real path, not the short one.
Thank you!
I made a dirty hack with a new script that I use to call my old script
PATH=/home/deploy/.rbenv/bin:/home/deploy/.rbenv/shims:$PATH
source "$1"
exit $?
More info in this diff
Hope this will be useful to somebody else :D

Why this shell won't work if it's called from rc.local but SSH?

I made this shell script
/var/start-app.sh
#!/bin/sh
# file-name: app-start.sh
cd /var/www/html/app
bundle exec rake sunspot:solr:start RAILS_ENV=production
unicorn_rails -c config/unicorn.rb -E production -D
If I type /var/start-app.sh in SSH. It triggers, and everything works fine.
Then I did put sh /var/start-app.sh in the end line of /etc/rc.d/rc.local in order to make it run on Server's start up.
But this never work. Why? and How can I enable that?
From SSH, I called it with root permission.
Is that why? If so, how can I give root permission to rc.local?
Looks like starting services from rc.local is not the best practice http://bencane.com/2011/12/30/when-its-ok-and-not-ok-to-use-rc-local/. You would be better writing an init script :
http://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/sysinit.html

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.

Shell Script not running properly

I have a linux shell script that when run from command line works perfectly but when scheduled to run via crontab, it does not give desired results.
The script is quite simple, it checks to see if mysql-proxy is running or not by checking if its pid is found using the pidof command. If found to be off, it attempts to start the proxy.
# Check if mysql proxy is off
# if found off, attempt to start it
if pidof mysql-proxy
then
echo "Proxy running."
else
echo "Proxy off ... attempting to restart"
/usr/local/mysql-proxy/bin/mysql-proxy -P 172.20.10.196:3306 --daemon --proxy-backend-addresses=172.20.10.194 --proxy-backend-addresses=172.20.10.195
if pidof mysql-proxy
then
echo "Proxy started"
else
echo "Proxy restar failed"
fi
fi
echo "==============================================="
The script is saved in a file check-sql-proxy.sh and has permissions set to 777. When I run the script from command line (sh check-sql-proxy.sh) it gives the desired output.
4066
Proxy running.
===============================================
The script is also scheduled to run every 5 minutes in crontab as
*/5 * * * * bash /root/auto-restart-mysql-proxy.sh > /dev/sql-proxy-restart-log.log
However, when I see the sql-proxy-restart-log.log file it contains the output:
Proxy off ... attempting to restart
Proxy restar failed
===============================================
It seems that pidof command fails to return the pid of the running application which brings the flow of script in else condition.
I am unable to figure out how to resolve this since when I run the script manually, it works fine.
Can anyone help what I am missing with regards to permissions or settings?
Thanks in advance.
Mudasser
Check that the shell is what you think it is (usually /bin/sh, not bash)
Also check that PATH environment variable. Usually, for cron jobs it is a good practice to fully qualify all paths to binaries, e.g.
#!/bin/bash
# Check if mysql proxy is off
# if found off, attempt to start it
if /bin/pidof mysql-proxy
etc.
Try pidof /usr/local/mysql-proxy/bin/mysql-proxy (full path to executable)
In common, try use the same command name as was used to start the instance of mysql-proxy.
The problem seems that crontab environment don't have the same environment as you.
You have 2 simple & proper solutions :
In the first lines of crontab :
PATH=/foo:/bar:/qux
SHELL=/bin/bash
or
source ~/.bashrc
in your scripts.

Resources