The reason why the script written in centos does not work in crontab - sh

I registered the script to crontab to run every 10 minutes. When I run the script, the log is printed, but if I put it on the cron tab and wait, the log does not appear, so it seems that it does not work. If you have anything I need to add or edit, please let me know :(
my sh script
#!/bin/sh
pslist=`ps -ef | grep ffprobe | awk '{print $2}'`
pscount=`ps -ef | grep ffprobe | wc -l`
logs='/apps/kMobile/kEncoderPy/batch/kill_ffprobe.log'
timestamp=`date +%Y%m%d--%H:%M`
echo "[$timestamp] Kill the Batch process Start :: "$timestamp >> $logs
echo "[$timestamp] process ffprobe running count :: "$pscount >> $logs
for pid in $pslist
do
rtime=`ps -p $pid -o etime | tail -1`
rtime=$rtime | tr -d ' '
minutes=${rtime:6:2}
if [$rtime != "ELAPSED" ]; then
if [ $minutes -gt 10 ]; then
`kill -9 $pid`
echo "[$timestamp] passed 10 minute kill process id : "$pid >> $logs
echo "[$timestamp] process kill after ffprobe running count : "$psount >> $logs
fi
fi
done
my crontab -e
*/10 * * * * /apps/kMobile/batch/kill_ffprobe.sh

Related

How to store hexdump of a string in hex format in powershell?

I am trying to convert a shell script to PowerShell script, I am facing issue with below shell commands
label=`echo -n "signing key" | hexdump -ve '/1 "%02x"'`
sign_nist="00000001${label}0000000100"
/testing$ echo $sign_nist
000000017369676e696e67206b65790000000100
/testing$ echo -n $sign_nist | sed -e 's/../\\x&/g'
\x00\x00\x00\x01\x73\x69\x67\x6e\x69\x6e\x67\x20\x6b\x65\x79\x00\x00\x00\x01\x00
/testing$ echo -ne "$(echo -n $sign_nist | sed -e 's/../\\x&/g')"
signing key
when I try to do the same thing in PowerShell using the below commands my output is different
PS C:\testing> $sign_nist
000000017369676E696E67206B65790000000100
PS C:\testing>
$prep = $sign_nist -split '(..)' -ne ''
for($i = 0; $i -lt $prep.length; $i++)
{
$prep[$i] = [System.Convert]::ToUInt32($prep[$i],16)
}
$sign_key_input=$prep -join '';
$sign_key_input
00011151051031101051101033210710112100010
could someone please help me how to get signing key to $sign_key_input with PowerShell?
Thanks in advance for the help.
I think appending \x for each byte works for linux to treat it as hex value but how to indicate the same in powershell?

Converting a shell script to PowerShell

After switching an application from Linux to Windows, I need to convert a shell script to a Windows equivalent. My choices were basically batch and PowerShell and I decided to give a shot to PowerShell.
For anyone interested, it's a local check for Check_MK to get information about SoftEther installed version and the number of sessions with performance data.
The initial shell script was as follow:
#!/bin/sh
cmd=$(/usr/local/vpnserver/vpncmd localhost:port /server /password:password /in:/usr/lib/check_mk_agent/local/vpncmd.txt)
version=$(echo "$cmd" | head -4 | tail -1)
sessions=$(echo "$cmd" | grep Sessions | awk '$1=$1' | cut -c21-22)
if [ -z "$version" ]; then
echo "3 VPN_Version - Can't get the information from vpncmd"
else
echo "0 VPN_Version - SoftEther VPN Server $version"
fi
if [ -z "$sessions" ]; then
echo "3 VPN_Sessions - Can't get the information from vpncmd"
else
echo "P VPN_Sessions sessions=$sessions;2;2"
fi
I basically got everything working except the 2 hardest lines of code:
cd "C:\Program Files\SoftEther VPN Server"
$cmd = vpncmd localhost:port /server /password:password /in:vpncmd.txt
$version=
$sessions=
if($version -eq $null) {
echo "3 VPN_Version - Can't get the information from vpncmd"
} else {
echo "0 VPN_Version - SoftEther VPN Server $version"
}
if($sessions -eq $null) {
echo "3 VPN_Sessions - Can't get the information from vpncmd"
} else {
echo "P VPN_Sessions sessions=$sessions;2;2"
}
I need help with going from the head, tail, grep, awk and cut one liners to whatever is equivalent in PowerShell. I read about Get-Content but I'm not sure if it's the most efficient way to do this and would like to prevent going from 1 line definition to 10 lines if that's possible to be as efficient in PowerShell.
Sample output of vpncmd's output: https://pastebin.com/J5FcHzHK
with the data being an array of lines & the word Version appearing multiple times in the actual source, the code needs to change a tad. in this version, it uses the way that -match works on an array to give the whole line as a result. that requires working on the output line to parse the desired data.
$Version = ($Vpncmd_Output -match '^Version \d{1,}\.\d{1,}' -split 'Version ' )[-1].Trim()
$SessionCount = [int]($Vpncmd_Output -match 'Number of Sessions\s+\|').Split('|')[-1].Trim()
$Version
$SessionCount
output ...
4.29 Build 9680 (English)
0
using the data in your PasteBin post, and presuming that is a multiline string, not an array of strings, this seems to work [grin] ...
$Vpncmd_Output -match '(?m)Number of Sessions\s+\|(?<Sessions>.*)'
$Matches.Sessions
# output = 0
$Vpncmd_Output -match '(?m)Version (?<Version>.+)'
$Matches.Version
# output = 4.29 Build 9680 (English)
i tried to combine the regex into one, but failed. [blush] the way i have it requires two passes, but it does work.

How to find files based on other files in current directory, with find command?

Want to find all mkv files without having same-name ass/srt file in the same folder.
How can I do that?
for example, I have following directory:
folder_1
|----folder_2
| |-----a.mkv
| |-----a.srt
|----folder_3
| |-----b.mkv
|----folder_4
|-----c.mkv
|-----c.ass
The search result should be: folder_1/folder_3/b.mkv.
Many Thanks.
Get answer from my friends, share it:
find . -name "*.mkv" -o -name "*.ass" -o -name "*.srt"| sort |rev|uniq -s 3 -u| rev|rgrep ".mkv"
BTW, if you are using synology nas, which does not have 'rev' command, you can walkaround it by using a python script(rev.py):
import sys
if __name__ == '__main__':
if len(sys.argv) >= 2:
for arg in sys.argv[1:]:
print '"' + arg[::-1] + '"'
and the script will be changed to:
find . -name "*.mkv" -o -name "*.ass" -o -name "*.srt"| sort |awk '{print "\"", $0,"\""}' OFS=""|xargs python rev.py |uniq -s 3 -u| xargs python rev.py | grep ".mkv\""

How to start 2 batch scripts in parralel and be able to ctrl+c them all?

What I need?
Run 3 commands from 1 batch script, and terminate them all with ctrl+c:
sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache
grunt watch
php -S 127.0.0.1:8081
What I tried:
start /b "sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache"
start /b "grunt watch"
start /b "php -S 127.0.0.1:8081"
And:
start /b "sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache"
start /b "grunt watch"
start /b "php -S 127.0.0.1:8081"
And:
start /b cmd /c "sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache"
start /b cmd /c "grunt watch"
start /b cmd /c "php -S 127.0.0.1:8081"
This one doesn't work, but I can only terminate Sass. When "grunt watch" hits - no longer able to terminate it.
Grunt:
> where grunt
> C:\Users\%USERNAME%\AppData\Local\scoop\apps\nodejs\0.12.0\nodejs\grunt
> C:\Users\%USERNAME%\AppData\Local\scoop\apps\nodejs\0.12.0\nodejs\grunt.cmd
> type C:\Users\%USERNAME%\AppData\Local\scoop\apps\nodejs\0.12.0\nodejs\grunt.cmd
> #IF EXIST "%~dp0\node.exe" (
> "%~dp0\node.exe" "%~dp0\node_modules\grunt-cli\bin\grunt" %*
> ) ELSE (
> #SETLOCAL
> #SET PATHEXT=%PATHEXT:;.JS;=;%
> node "%~dp0\node_modules\grunt-cli\bin\grunt" %*
>)
Sass:
> where sass
> C:\Users\%USERNAME%\AppData\Local\scoop\apps\ruby\2.1.5\bin\sass.bat
> type C:\Users\%USERNAME%\AppData\Local\scoop\apps\ruby\2.1.5\bin\sass.bat
> #ECHO OFF
> IF NOT "%~f0" == "~f0" GOTO :WinNT
> #"ruby.exe" "C:/Users/%USERNAME%/AppData/Local/scoop/apps/ruby/2.1.5/bin/sass" %1 %2 %3 %4 %5 %6 %7 %8 %9
> GOTO :EOF
> :WinNT
> #"ruby.exe" "%~dpn0" %*
I also found this: Powershell: Run multiple jobs in parralel and view streaming results from background jobs
But it's:
1. PowerShell, not plain cmd.exe
2. Too difficult for me to understand.
But still, if cmd isn't capable of running 3 cmds at once, I might switch to PowerShell. So PS solutions are acceptable too.
Thank you.
UPD
Just for clarification. In bash I do traps: https://stackoverflow.com/a/3414377/898099 . And I get the logs with them. Need the same for windows.
Here's a powershell solution for you. You can use powershell jobs to kick off your processes and intercept the Ctrl+C and use it instead to handle the stop, retrieval of output, and removal of the jobs that were created:
$job1 = Start-Job { sass -w styles/scss/main.scss:styles/css/main.css --style compressed --no-cache }
$job2 = Start-Job { grunt watch }
$job3 = Start-Job { php -S 127.0.0.1:8081 }
Write-Host "Jobs started. Ctrl+C to terminate"
[console]::TreatControlCAsInput = $true
while($true) {
Sleep -milliseconds 500
if ([console]::KeyAvailable) {
$key = [system.console]::readkey($true)
if (($key.modifiers -band [consolemodifiers]"control") -and ($key.key -eq "C")) {
break;
}
}
}
Stop-Job $job1, $job2, $job3
$job1, $job2, $job3 | Foreach-Object {
Write-Host "Job $($_.id):"
Receive-Job $_
Remove-Job $_
Write-Host
}
If you're not bothered about what the output of these jobs is, you can remove the line that says Receive-Job $_

echo -n not working with secondary operation

I have a few filenames in a directory
blabla.01
blabla.02
...
I'm trying to make a new file with the following format:
01 new stuff here
02 more new stuff
...
I wrote a script and dumbed it down a bit:
#!/bin/bash
FILES=$(find . -type f -name "blabla*" | awk -F'[.]' '$(NF-1)>=1' | sort)
for f in $FILES
do
echo -n $f | cut -d "." -f 3
echo "test"
done
the 'test' will be the output of another code..
However in this example i get something like:
01
test02
test
Thanks
Try
printf '%s%s\n' "$(echo "$f" | cut -d . -f3)" "test"
The echo | cut could probably be replaced with something like "${f##*.}" if you always want the laat field.