check autosys job status via unix without using autorep command - shell

I am trying to fetch the status of autosys job via shell script without using autorep command.
I/P:
user id PID Env(AUTOSYS_JOB_NAME="test_job",script_location="/tmp/SA/test.sh")
From the above command I wanted to fetch the following details:
run id
the job success/failure/in progress everytime
script location
start time
end time
when the job is running and its getting overwritten in my sql server db under following table.
I have tried
ps aux | grep -v grep | grep 'user_id' | grep 'AUTOSYS_JOB_NAME'
The above command is not working as expected

Related

Stopping a task in shell script if logs contain specific string

I am trying to run a command in shell script and would like to exit it if the processing logs (not sure what you call the logs that are outputted on terminal while the task is running) contains the string "INFO | Next session will start at"
I tried using grep but because the string "INFO | Next session will start at" is not in a stdout it does not detect while the command is running.
The specific command I'm running is below
pipenv run python3 run.py --config accounts/user/config.yml
By 'processing logs' I mean the log output before the stdout is displayed in the terminal.
...
[D 211127 10:07:12 init:400] atx-agent version 0.10.0
[D 211127 10:07:12 init:403] device wlan ip: route ip+net: no such network interface
[11/27 10:07:12] INFO | Time delta has set to 00:11:51.
[11/27 10:07:13] INFO | Kill atx agent.
[11/27 09:59:32] INFO | Next session will start at: 10:28:30 (2021/11/27).
[11/27 09:59:32] INFO | Time left: 00:28:57.
I am trying to do this because the yml file I'm trying to run has a limit on what time you can execute it, and I would like to exit the task if the time is not met.
I tried to give as much context but if there's something missing please let me know.
This may work:
pipenv run python3 run.py --config accounts/user/config.yml |
sed "/INFO | Next session will start at/q"
sed prints the piped input, until it matches the expression and quits (q). The program will receive SIGPIPE (broken pipe) when it tries to continue writing, and (likely) exit. It's the same as what happens when you do something like find | head.
You could also use kill in a shell wrapper:
sh -c 'pipenv run python3 run.py --config accounts/user/config.yml |
{ sed "/INFO | Next session will start at/q"; kill -- -$$; }'
Notes:
The program may print a different log if stdout is not a terminal.
If you want to match a literal string, you could use grep -Fm 1 PATTERN, but other log output will be hidden. grep fails if no match, which can be useful.
This will work any shell, including zsh. zsh or bash can also be used for the kill wrapper.
There are other approaches. This thread focuses on tail, but is a useful reference: https://superuser.com/questions/270529/monitoring-a-file-until-a-string-is-found

Challenge in setting up the script which connects to multiple servers as an autosys job

The purpose of the script is to login to multiple servers and execute the df -k command and to send out email containing the list similar like below :
Thu Nov 3 12:59:49 EDT 2016
Running out of space "/opt (80%)" on (a******001s02)
Running out of space "/var (83%)" on (a*******01s01)
Running out of space "/opt/IBM/ITM (98%)" on (a*******001s01)
Running out of space "/apps (80%)" on (a*********01s01)
Running out of space "/opt/wily (80%)" on (a********01s01)
My challenge is :
This script has to run as a scheduled job like autosys job . And the challenge is that, what i believe(not 100 percent sure) , the username ( ssh $username#$host)
should be a sudo user for it to run as an autosys job. But if i hardcode username as a sudo user , the ssh command wont work . As in , we cannot login to multiple servers with username as sudo.
Here I have used 'logname' , so logname will take the name of the user executing the command .But , logname doesnt seem to work if i have to make it as autosys job .
Basically what I want is to make this script run as an autosys job. Currently its working perfectly fine If I can manually run the script .I appreciate any help. Thanks in advance :)
Entire script:
#Shell script to watch disk space
#!/bin/sh
MAILTO="xxxxxxx#xxxxx.com"
# Set alert level 80% is default
ALERT=70
path=/tmp/Joshma
report=$path/report.txt
date > $report
echo >>$report
servers=$path/serversfile.txt
username=`logname`
for host in `cat $servers`
do
echo "$host"
ssh $username#$host df -k | grep -vE '^Filesystem|tmpfs|cdrom|proc' | awk '{ print $4 " " $7 }' | while read output;
do
echo " $output"
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge $ALERT ]
then
echo "Running out of space \"$partition ($usep%)\" on ($host)\n" >>$report
fi
done
done
cat $report | mail -s "File System Space Check" $MAILTO
The job owner value in a Autosys job can be any user ID that can logon to the server (The machine value) that is executing the AutoSys job. The AutoSys agent will spawn a child process that executes the command as that user ID. Use the ID that you are using to manual run the script as the job owner if you can. If not make sure that the ID that you use has the appropriate permissions to ssh to your server and execute the commands. It is not a AutoSys requirement that the job owner value have sudo on a server.
If the script runs manually but fails in the AutoSys job using the same ID and server, start looking at the environment set in child process. By default AutoSys sources a generic profile that may not be the same as the one set when logging in manually with the ID. You may have to set the profile: value in the job to source a profile file to set the environment correctly when the child process is started.

PS command does not show running process

I have very long running shell script ( runs more than 24 hours) .
It is very simple script. It just reads xml file from a dir and perform sed operation of the content of the file. There are 1 millions xml files in the dir.
My script name is like runDataManipulation.sh
When I run following command
ps -ef | grep "runDa*"
then sometime I see my process as
username 34535 1 48 11:42:01 - 224:22 /usr/bin/ksh ./runDataManipulation.sh
But if I run exactly same command after couple of seconds then I don't see above process at all.
As my process is running all the time so I expect that ps command to show the process all the time.
If I do grep on the process id of my script like ..
ps -ef | grep 34535
then sometime I see result like
username 34535 1 51 11:42:01 - 229:22 [ksh]
sometime I see result like
username 45678 34535 0 14:12:11 - 0:0 [sed]
My main questions is that ... why do I not see my process when I grep for my process using script name. I am using AIX 6.1.
It looks to me like one script is spawning off your script in another process.
If you look at the results of your ps command below the first line is showing the process id of 34535, this is the main id (say parent id).
username 34535 1 51 11:42:01 - 229:22 [ksh]
This process in turn is firing off another process, this can be seen below, notice the id of the parent process (34535) is mentioned in the line below, the first number is the main process Id and the second number is the calling process.
username 45678 34535
If you changed your ps command to include the sed command you should always see some results if your scripts are still running!

Mail command executing commands inside of string passed in as message body

Ive got a script that checks if a process is running or not, using ps -ef and some grep. If the process is running, it does nothing. If the process isnt running, it restarts the process, and then sends an email to me stating that the process died.
This script currently runs ever 5 minutes from a admin account's crontab.
Peusdocode + problem code:
#!/bin/sh
# declare a ton of environment variables.
ATONOFVARS=/lots/of/qualified/paths
# declare relevant logging functions
loggingFunction()
# declare failure message to pass into mail (real code)
PAGEMESSAGE="MyServer: process not found in ps -ef. Attempting to Restart... Please check log file in /tmp"
PAGESUBJECT="ProcessHealthCheck.sh - MyServer: process not found in ps -ef"
EMAILLIST="my.email#mycorp.com"
# do the actual check (back to peusdo code)
if [ $(ps -ef | grep process | grep adminAccount | grep -v grep | wc -1) ]
restartProcess()
FAILURE=1
fi
# log the failure and send an email to me (real code)
if [ $FAILURE -eq 1 ]
then
logMsg ""
logMsg "The process is not detected in ps -ef!"
logMsg "Restarting..."
logMsg ""
#send emails out
echo $PAGEMESSAGE | mail -s $PAGESUBJECT $EMAILLIST
fi
To test this, I deliberately killed the PID of my process I want monitored, so this cronjob would have to restart it, I also wanted to test the email capability. The script restarts my process exactly like I want it, and I get the email.
However the email says in the message body: "MyServer: process not found in
the entire listing of ps -ef. Attempting to Restart... Please check log file in /tmp". Also, the addresses that the email was sent to, are lines from ps -ef as well. As in my email had 2000 recipients. Sample addresses: grep#myServer.mydomain.com, process#myServer.mydomain.com
Does anyone know whats going on? Why is mail executing a command that is in a string? Since finding this out, I have changed the string to remove any possibility of a unix command.

Checking if user has process running

I have been using this to check if the process i want to edit is already running.
Now this returns if any user has this process running, but since multiple users now run it, i need this line to only return true if the current user has it running. I already have something to execute something as_user and the username is saved in ME.
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
$LOGNAME provides the current user name. So in case you are using the command to run from X user and want to check for that specific user process, then you can add additional grep for $LOGNAME. I am using SUSE-Linux. In case you are using any other OS, Please specify.

Resources