Command for Job status history in Autosys - autosys

Can we get the status of an autosys job for the past 20 days. not the -r command which given that particular day. the whole 20 days.

I think -r can give you what you want. Try executing below:
autorep –j <your job name> -r -19

I give you AUTOHIST.cmd Slow as heck but works from command line.
#echo off
SET JOB_NAME=%~1
IF [%JOB_NAME%]==[] GOTO Usage
SET NUM_ENTRIES=%~2
IF [%NUM_ENTRIES%]==[] SET NUM_ENTRIES=10
SET /A NUM_ENTRIES=%NUM_ENTRIES%-1
SET HIST_START=%~3
IF [%HIST_START%]==[] SET HIST_START=0
SET SKIP_HEADER=0
FOR /L %%R IN (%HIST_START%, 1, %NUM_ENTRIES%) DO (
CALL :HistoricalAutoRep %%R
)
GOTO :EOF
:HistoricalAutoRep
IF [%SKIP_HEADER%]==[0] (
SET OPTIONS="delims="
) ELSE (
SET OPTIONS="skip=3 delims="
)
FOR /F %OPTIONS% %%F IN ('CALL AUTOREP -J %JOB_NAME% -r -%1') DO ECHO %%F
SET SKIP_HEADER=1
GOTO :EOF
:Usage
ECHO AUTOHIST ^<Required job name^> [Optional number of historic runs to return] [Optional number of runs back to start querying history]
GOTO :EOF

Related

zsh/bash source command behavior difference

I am trying to source a third party script in zsh (named setup_env.sh stored in ~/), that has following lines in the beginning to guard against accidental execution:
#!/bin/sh
# Guard the script against execution - it must be sourced!
echo $0 | egrep 'setup_env.sh' > /dev/null
if [ $? -eq 0 ]; then
echo ""
echo "ERROR: the setup file must be SOURCED, NOT EXECUTED in a shell."
echo "Try (for bash) : source setup_env.sh"
echo "Or (eg. for ksh): . setup_env.sh"
exit 1
fi
# export some environment variables
...
When I source this script with source ~/setup_env.sh, I see the error message shown in the above code block.
From the script it's apparently visible that it's not written with zsh in mind. But I still want to know why zsh behaves this way, and if it's possible to source the script as it is.
I could source the script as it is without error using bash.
I could also source it in zsh after commenting out the guard block in the beginning of the script.
Can someone explain this difference in behavior for source command between zsh and bash?
zsh/bash have different ways to detect sourcing, following should work for both :
if [[ -n $ZSH_VERSION && $ZSH_EVAL_CONTEXT == toplevel ]] || \
[[ -n $BASH_VERSION && $BASH_SOURCE == $0 ]]; then
echo "Not sourced"
exit 1
fi
To explain a little more, when you run :
source setup_env.sh
# setup_env.sh containing "echo $0"
In zsh, $0 == setup_env.sh
In bash, $0 == bash

How to get yesterday's date in a particular format?

I need to set Today and yesterday's date in a variable in a fixed format YYYYMMDD.
For today date, when i did
SET TODAY=%date:~10,4%%date:~4,2%%date:~7,2%
it worked and displayed '20190426'.
But how to set yesterday's date so I get it in the format - 20190425 ?
Update The original unix and linux tags were later changed to cmd and batch-file, which this Linux / Bash / sh solution won't apply to.
To get yesterday's date:
$ date +%Y%m%d --date yesterday
20190425
To get it into a var:
$ var=$(date +%Y%m%d --date yesterday)
$ echo $var
20190425
There are literally hundreds/thousands of questions just here on SO.
I suggest you use a PowerShell one-liner for this, which you can call from a batch file as follows:
#echo off
for /f "usebackq delims=" %%A in (`
powershell -NoP -C "'{0:yyyyMMdd}' -f (Get-Date).AddDays(-1)"
`) do set YESTERDAY=%%A
%YESTERDAY% will then contain 20190824 when invoked on 25 August 2019, for instance.
A slightly longer variant, incorporating both today and yesterday in only one PowerShell invocation.
:: Q:\Test\2019\04\26\SO_55862158.cmd
#echo off
for /f "usebackq delims=" %%A in (`
powershell -NoP -C "'yesterday={0:yyyyMMdd}' -f (Get-Date).AddDays(-1);'today={0:yyyyMMdd}' -f (Get-Date)"
`) do set "%%A"
The PowerShell part issues two lines
yesterday=20190824
today=20190825
which are parsed by the for /f and set as environment variables yesterday/today respectivly.
You can use this batch/vbs hybrid, you need to save it as a .bat or .cmd extension file.:
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "yyyy=%result:~0,4%"
set "mm=%result:~4,2%"
set "dd=%result:~6,2%"
set "final=%yyyy%%mm%%dd%"
echo %final%
Note, you can toggle the number of days in the set day=-1 line.
Previously posted answers are not pure Batch-file solutions... You may use the method explained at this answer or just use this simpler approach:
#echo off
setlocal
set /A "YYYY=%date:~10,4%, MM=1%date:~4,2%, M=MM-100, DD=1%date:~7,2%, D=DD-100"
echo TODAY: %YYYY%%MM:~1%%DD:~1%
set /A "C1=!(D-=1),M-=C1*(1-12*(C2=!(M-1))),YYYY-=C1*C2,MM=100+M,DD=100+(D+=C1*(30+((M+(M>>3))&1)-!(M-2)*(2-!(YYYY%%4))))"
echo YESTERDAY: %YYYY%%MM:~1%%DD:~1%

Bash menu script - Cannot execute commands

I am building a minimal standard bash menu. The selections works fine but commands are not being executed. I suspect I might to wrap in the command, but not sure how that is being done. I will often have 2 commands sent in, thus the commands needs to be separated in a way that bash understand it as line of commands.
I found this SO question with a similar title, but not answering why commands inside the bash script are not being executed:
Cannot execute shell commands in bash script
What works and not?
Press 1: Does not change to correct cd.
Press 2: Creates the file in correct folder.
Press 3: Works.
Press 4: Works (R file is prepared with: a <- 1).
Wanted behaviour:
I need the commands inside the bash menu script, to be executed.
#!/bin/bash
# -----
# Menu:
# -----
while :
do
echo "Menu"
echo "1 - Change directory to /tmp"
echo "2 - Create file test1.sh with hello message, in /tmp"
echo "3 - Execute test1.sh"
echo "4 - Execute R-script [/tmp/test2.R]"
echo "Exit - any kind but not [1-4]"
read answer;
case $answer in
1)
echo "Change directory to [\tmp]"
cd /tmp # Command to be executed.
break
;;
2)
echo "Create file [test1.sh] in [\tmp]"
# Commands to be executed.
cd /tmp
touch test1.sh
chmod +x test1.sh
echo echo "hello" > test1.sh
break
;;
3)
echo "Execute file [test1.sh]"
/tmp/./test1.sh # Command to be executed.
break
;;
4)
echo "Execute R-script [/tmp/test2.R]"
cd /tmp && /usr/bin/Rscript test2.R # Command to be executed.
break
;;
*)
# Command goes here
echo "Exit"
break
;;
esac
done
you should not use break when you want to execute all cases.
and also in case 3 you used /tmp/./test1.sh it should be ./tmp/test1.sh or sh /tmp/test1.sh
your code should be:
#!/bin/bash
# -----
# Menu:
# -----
while :
do
echo "Menu"
echo "1 - Change directory to /tmp"
echo "2 - Create file test1.sh in /tmp"
echo "3 - Execute test1.sh"
echo "4 - Execute R-script [/tmp/test2.R]"
echo "Exit - any kind but not [1-4]"
read answer;
case $answer in
1)
echo "Change directory to [\tmp]"
cd /tmp # Command to be executed.
pwd
;;
2)
echo "Create file [test1.sh] in [\tmp]"
touch /tmp/test1.sh # Command to be executed.
;;
3)
echo "Execute file [test1.sh]"
./tmp/test1.sh # Command to be executed.
;;
4)
echo "Execute R-script [/tmp/test2.R]"
/usr/bin/Rscript /production/20_front_trader/build/x_run_front_trader.R # Command to be executed.
;;
*)
# Command goes here
echo "Exit"
;;
esac
done

How to write an output of multiple simultaneous R script into a single file?

For my simulations, I wrote a little loop to run multiple (n=20) instances of "my_script.R" simultaneously:
#! /bin/bash
declare -i i=1 n=20
while [ $i -le $n ]; do
echo "#!/bin/bash --login" >my.qsub.${i}
echo "#PBS -l nodes=1:ppn=1" >> mu.qsub.${i}
echo "#PBS -l mem=2GB" >> my.qsub.${i}
echo "cd /~path to my wd/" >> my.qsub.${i}
echo "module load R/3.0.1" >> my.qsub.${i}
echo -n 'R CMD BATCH --vanilla --slave --no-timing my_script.R ' >> my.qsub.${i}
echo "" >> my.qsub.${i}
qsub -l walltime=03:59:00 my.qsub.${i}
sleep 2
let i+=1
done
Within "my_script.R", I have a for loop (n=1,...,B), which writes it's results into a "fnam" .txt file in the end of each iteration:
cat(file=fnam, append=TRUE, Results[n,], "\n")
Everything works fine, BUT it looks like there may be a problem if two different instances of the same R scrip try to append the fnam file simultaneously.
Did anyone try to synchronize/order the way, in which multiple instances of the same R script append the same output file?
They jobs will need to write to different files which you will have to combine after. You can submit all the jobs at once as as 'Array Job' using qsub -t 20 -sync y. That will create 20 identical jobs and wait for all of them to finish before returning. Each job can get a unique identifier for itself via the environment variable SGE_TASK_ID from which it can craft a unique filename. The sync option will make qsub wait until they complete before returning and then you can concatenate all the files together.

Batch file: get file timestamp date only and age in days

The following few lines output the names of certain files in a folder, a delimiter, and a timestamp.
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* *.msg 2^>nul'
) do echo %indent%%fileBullet% %%F%delimeter% %%~tF
So, produces something like this
Response.docx; 02/07/2013 12:13 PM
I'd like to remove the time portion of the timestamp (so date only), followed by how many days old the file is. So
Response.docx; 02/07/2013; 14
I've found some fairly lengthy solutions online that contain a dozen or so lines. Is there a short and sweet approach?
Here's something shorter and sweeter. It's not as short and sweet as you'd like, but at least it's not 12 lines of code. :)
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* *.msg 2^>nul'
) do call :datediff "%indent%%fileBullet% %%F%delimeter%" %%~tF
goto :EOF
:datediff
echo wscript.echo DateDiff^("d", "%2", Date^(^)^)>"%temp%\dd.vbs"
set /P i="%~1 %2%delimeter% "<NUL
cscript /nologo "%temp%\dd.vbs"
del /q "%temp%\dd.vbs"

Resources