I have this specific file:
Client 1: MAC 00:03:52:49:99:55
First : Fri Nov 7 09:50:11 2014
Last : Fri Nov 7 09:51:06 2014
--
Client 1: MAC 00:03:52:04:88:55
First : Fri Nov 7 09:51:44 2014
Last : Fri Nov 7 09:51:44 2014
--
Client 2: MAC 00:03:52:49:99:55
First : Fri Nov 7 10:50:10 2014
Last : Fri Nov 7 10:50:10 2014
--
Client 3: MAC 00:03:52:04:66:55
First : Fri Nov 7 09:51:30 2014
Last : Fri Nov 7 09:51:30 2014
--
From this file with many duplicate items like to create a new file like this:
00:03:52:49:99:55
First : Fri Nov 7 09:50:11 2014
Last : Fri Nov 7 09:51:06 2014
First : Fri Nov 7 09:50:11 2014
Last : Fri Nov 7 09:51:06 2014
00:03:52:04:88:55
First : Fri Nov 7 09:51:44 2014
Last : Fri Nov 7 09:51:44 2014
00:03:52:04:66:55
First : Fri Nov 7 09:51:30 2014
Last : Fri Nov 7 09:51:30 2014
How i can search with Bash Script the File with a For-Loop ? Important that the loop don't make more then 1 entry for the MAC-address. The MAC should be unique.
yes i have tried all this day :/
#!/bin/bash
array=$(cat Kismet-20141107-09-48-19-1.nettxt | grep Client -A 3 | grep -v Manuf)
echo "Array size: ${#array[#]}"
echo "Array items:"
for item in ${array[*]}
do
if [ $item -eq 3 ]; then
echo "$array[$item]"
fi
done
no it's not a requirement to use bash.. if you have other tools i will try it!
Try this out:
Shell> cat test1
#!/bin/bash
MACS=(`grep Client file|awk '{print $4}'|sort|uniq|xargs`)
for i in `echo ${MACS[*]}`; do
echo $i
grep $i file -A 2 | grep -vE 'MAC|--'
done
Shell> cat file
Client 1: MAC 00:03:52:49:99:55
First : Fri Nov 7 09:50:11 2014
Last : Fri Nov 7 09:51:06 2014
--
Client 1: MAC 00:03:52:04:88:55
First : Fri Nov 7 09:51:44 2014
Last : Fri Nov 7 09:51:44 2014
--
Client 2: MAC 00:03:52:49:99:55
First : Fri Nov 7 10:50:10 2014
Last : Fri Nov 7 10:50:10 2014
--
Client 3: MAC 00:03:52:04:66:55
First : Fri Nov 7 09:51:30 2014
Last : Fri Nov 7 09:51:30 2014
--
Output:
Shell> ./test1
00:03:52:04:66:55
First : Fri Nov 7 09:51:30 2014
Last : Fri Nov 7 09:51:30 2014
00:03:52:04:88:55
First : Fri Nov 7 09:51:44 2014
Last : Fri Nov 7 09:51:44 2014
00:03:52:49:99:55
First : Fri Nov 7 09:50:11 2014
Last : Fri Nov 7 09:51:06 2014
First : Fri Nov 7 10:50:10 2014
Last : Fri Nov 7 10:50:10 2014
Related
I have a log file in which new lines are continuously written.
I would like a bash script that continuously reads the last line of this log file, so that I can process the line (e.g. execute a specific command if the line contains the word "error").
I've tried:
while true
do
if tail -n1 -f file.log | grep -q ERROR
then
echo "$(date) : ERROR detected"
fi
done
But it's spamming:
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
sun 21 mar 2021 18:32:41 CET : ERROR detected
(a new line is added every minute in this example)
How can I read only the last line and do not have spam for the result ?
I suggest with GNU grep:
tail -n1 -f file.log | grep --line-buffered ERROR | while read; do echo "$(date) : ERROR detected"; done
This is exactly why tail -f has been invented:
tail -f <logfile>
will show the last line of your logfile, so you can follow what gets added.
This can be combined with a grep:
tail -f <logfile> | grep <text_to_be_searched>
In your case:
tail -f file.log | grep "ERROR"
I need to parse all a file into a better format to produce an outcome with columns delimited by a comma, thinking of being able to export the content in CSV file.
This is an example of my input;
. D 0 Mon Dec 10 11:07:46 2018
.. D 0 Mon Feb 19 11:38:06 2018
RJ9-5 D 0 Fri Nov 30 10:34:24 2018
WorkingOnClass D 0 Wed Feb 28 09:37:52 2018
ML-Test001 D 0 Fri Dec 7 16:38:56 2018
TestML4Testing D 0 Wed Aug 22 08:58:42 2018
ML-NewDataSE SetCases1.xlsx A 1415577 Wed Aug 29 14:00:16 2018
DR0001-Dum01 D 0 Thu Aug 16 08:24:25 2018
DR0002-Dum02 D 0 Thu Aug 16 09:04:50 2018
Readme File for Documentation And Data Description.docx A 16136 Wed Aug 29 14:00:24 2018
ML Database Prototype D 0 Thu Dec 6 15:11:11 2018
OneNote D 0 Mon Dec 3 09:39:20 2018
Data A 0 Mon Dec 10 11:07:46 2018
\RJ9-5
. D 0 Fri Nov 30 10:34:24 2018
.. D 0 Mon Dec 10 11:07:46 2018
KLR0151_Set023_Files_RJ9_05.xlsx A 182462 Wed Apr 4 02:48:55 2018
KLR0152_Set023_Files_RJ9_05.xlsx A 525309 Wed Apr 4 02:53:57 2018
\ML-Test001
. D 0 Wed Feb 28 09:37:52 2018
.. D 0 Mon Dec 10 11:07:46 2018
WT_Conforming_Format1_1.docx A 500914 Mon Feb 26 08:50:55 2018
Conforming_Format_1_1.xlsx A 130647 Mon Feb 26 08:52:33 2018
DR0135_Dum01_text.xls A 974848 Mon Feb 12 08:11:11 2018
DR0139_Dum02_body.xls A 1061888 Tue Jun 19 13:43:54 2018
DataSet_File_mod0874953.xlsx A 149835 Mon Feb 26 14:17:02 2018
File Path For Dataset-2018.07.11.xlsx A 34661 Mon Feb 12 09:27:17
This is script right here can make the job:
#!/bin/bash
awk -v OFS=, '
BEGIN { print "PATH, FILENAME, SIZE, TIMESTAMP" }
/[\\]/ { path=$0 }
$2 ~ /A/ {print path"\\"$1,$3,$4 " " $5 " " $6 " " $7 " "$8 }
' "$#"
But is ignoring the names with spaces on it, so I need to validate them with something like:
awk -v FS="\t" '{print $1}'
But I could't integrate into the shell script, because the way the shell script is working, so I was thinking on make AWK to start reading by the end, since the end is always the same, and leave the rest.
The output should something like this:
/RJ9-5/KLR0151_Set023_Files_RJ9_05.xlsx,182462,Wed Apr 4 02:48:55 2018
/RJ9-5/KLR0152_Set023_Files_RJ9_05.xlsx,25309,Wed Apr 4 02:53:57 2018
/ML-Test001/WT_Conforming_Format1_1.docx,500914,Mon Feb 26 08:50:55 2018
/ML-Test001/Format_1_1.xlsx,130647,Mon Feb 26 08:52:33 2018
/ML-Test001/DR0135_Dum01_text.xls,974848,Mon Feb 12 08:11:11 2018
/ML-Test001/DR0139_Dum02_body.xls,1061888,Tue Jun 19 13:43:54 2018
/ML-Test001/DataSet_File_mod0874953.xlsx,149835,Mon Feb 26 14:17:02 2018
/ML-Test001/File Path For Dataset-2018.07.11.xlsx,34661,Mon Feb 12 09:27:17 2018
With GNU awk for the 3rd arg to match() (and far less importantly \s shorthand for [[:space:]]):
$ cat tst.awk
BEGIN { OFS="," }
{ gsub(/^\s+|\s+$/,"") }
sub(/^\\/,"/") { path = $0; next }
path == "" { next }
match($0,/^(.*[^ ]) +A +([^ ]+) +(.*)/,a) { print path "/" a[1], a[2], a[3] }
$ awk -f tst.awk file
/RJ9-5/KLR0151_Set023_Files_RJ9_05.xlsx,182462,Wed Apr 4 02:48:55 2018
/RJ9-5/KLR0152_Set023_Files_RJ9_05.xlsx,525309,Wed Apr 4 02:53:57 2018
/ML-Test001/WT_Conforming_Format1_1.docx,500914,Mon Feb 26 08:50:55 2018
/ML-Test001/Conforming_Format_1_1.xlsx,130647,Mon Feb 26 08:52:33 2018
/ML-Test001/DR0135_Dum01_text.xls,974848,Mon Feb 12 08:11:11 2018
/ML-Test001/DR0139_Dum02_body.xls,1061888,Tue Jun 19 13:43:54 2018
/ML-Test001/DataSet_File_mod0874953.xlsx,149835,Mon Feb 26 14:17:02 2018
/ML-Test001/File Path For Dataset-2018.07.11.xlsx,34661,Mon Feb 12 09:27:17
Try this Perl solution:
$ perl -lane ' if(/^\s*$/) { $x=0;$y=0} if(/^\\/) {$x=1 ;($a=$_)=~s/\s*$//g;$a=~s/\\/\//g; } $y++ if $x==1 ; if($y>3) { s/^\s*//g; $_=~s/(.+?)\s+\S+\s+((\d+)\s+.+)/$1 $2/g;print "$a/$_" } ' essparaq.txt
/RJ9-5/KLR0151_Set023_Files_RJ9_05.xlsx 182462 Wed Apr 4 02:48:55 2018
/RJ9-5/KLR0152_Set023_Files_RJ9_05.xlsx 525309 Wed Apr 4 02:53:57 2018
/ML-Test001/WT_Conforming_Format1_1.docx 500914 Mon Feb 26 08:50:55 2018
/ML-Test001/Conforming_Format_1_1.xlsx 130647 Mon Feb 26 08:52:33 2018
/ML-Test001/DR0135_Dum01_text.xls 974848 Mon Feb 12 08:11:11 2018
/ML-Test001/DR0139_Dum02_body.xls 1061888 Tue Jun 19 13:43:54 2018
/ML-Test001/DataSet_File_mod0874953.xlsx 149835 Mon Feb 26 14:17:02 2018
/ML-Test001/File Path For Dataset-2018.07.11.xlsx 34661 Mon Feb 12 09:27:17
$ cat essparaq.txt
. D 0 Mon Dec 10 11:07:46 2018
.. D 0 Mon Feb 19 11:38:06 2018
RJ9-5 D 0 Fri Nov 30 10:34:24 2018
WorkingOnClass D 0 Wed Feb 28 09:37:52 2018
ML-Test001 D 0 Fri Dec 7 16:38:56 2018
TestML4Testing D 0 Wed Aug 22 08:58:42 2018
ML-NewDataSE SetCases1.xlsx A 1415577 Wed Aug 29 14:00:16 2018
DR0001-Dum01 D 0 Thu Aug 16 08:24:25 2018
DR0002-Dum02 D 0 Thu Aug 16 09:04:50 2018
Readme File for Documentation And Data Description.docx A 16136 Wed Aug 29 14 :00:24 2018
ML Database Prototype D 0 Thu Dec 6 15:11:11 2018
OneNote D 0 Mon Dec 3 09:39:20 2018
Data A 0 Mon Dec 10 11:07:46 2018
\RJ9-5
. D 0 Fri Nov 30 10:34:24 2018
.. D 0 Mon Dec 10 11:07:46 2018
KLR0151_Set023_Files_RJ9_05.xlsx A 182462 Wed Apr 4 02:48:55 2018
KLR0152_Set023_Files_RJ9_05.xlsx A 525309 Wed Apr 4 02:53:57 2018
\ML-Test001
. D 0 Wed Feb 28 09:37:52 2018
.. D 0 Mon Dec 10 11:07:46 2018
WT_Conforming_Format1_1.docx A 500914 Mon Feb 26 08:50:55 2018
Conforming_Format_1_1.xlsx A 130647 Mon Feb 26 08:52:33 2018
DR0135_Dum01_text.xls A 974848 Mon Feb 12 08:11:11 2018
DR0139_Dum02_body.xls A 1061888 Tue Jun 19 13:43:54 2018
DataSet_File_mod0874953.xlsx A 149835 Mon Feb 26 14:17:02 2018
File Path For Dataset-2018.07.11.xlsx A 34661 Mon Feb 12 09:27:17
This question already has answers here:
Bash script/command to print out date 5 min before/after
(4 answers)
Closed 5 years ago.
I want to add 10 seconds 10 times. But I don't know well how to add times to the value.
This is my code.
./time.sh
time=$(date)
counter=1
while [ $counter -le 10 ]
do
echo "$time"
time=$('$time + 10 seconds') //error occurred.
((counter++))
done
echo All done
Using GNU Date
Assuming GNU date, replace:
time=$('$time + 10 seconds')
with:
time=$(date -d "$time + 10 seconds")
Putting it all together, try:
$ cat a.sh
t=$(date)
counter=1
while [ "$counter" -le 10 ]
do
echo "$t"
t=$(date -d "$t + 10 seconds")
((counter++))
done
echo All done
(I renamed time to t because time is also a bash built-in command and it is best to avoid potential confusion.)
When run, the output looks like:
$ bash a.sh
Tue Jan 16 19:19:44 PST 2018
Tue Jan 16 19:19:54 PST 2018
Tue Jan 16 19:20:04 PST 2018
Tue Jan 16 19:20:14 PST 2018
Tue Jan 16 19:20:24 PST 2018
Tue Jan 16 19:20:34 PST 2018
Tue Jan 16 19:20:44 PST 2018
Tue Jan 16 19:20:54 PST 2018
Tue Jan 16 19:21:04 PST 2018
Tue Jan 16 19:21:14 PST 2018
All done
Using Bash (>4.2)
Recent versions of bash support date calculations without external utilities. Try:
$ cat b.sh
#!/bin/bash
printf -v t '%(%s)T' -1
counter=1
while [ "$counter" -le 10 ]
do
((t=t+10))
printf '%(%c)T\n' "$t"
((counter++))
done
echo All done
Here, t is time since epoch in seconds.
When run, the output looks like:
$ bash b.sh
Tue 16 Jan 2018 07:31:44 PM PST
Tue 16 Jan 2018 07:31:54 PM PST
Tue 16 Jan 2018 07:32:04 PM PST
Tue 16 Jan 2018 07:32:14 PM PST
Tue 16 Jan 2018 07:32:24 PM PST
Tue 16 Jan 2018 07:32:34 PM PST
Tue 16 Jan 2018 07:32:44 PM PST
Tue 16 Jan 2018 07:32:54 PM PST
Tue 16 Jan 2018 07:33:04 PM PST
Tue 16 Jan 2018 07:33:14 PM PST
All done
I have a script that will check our servers for print jobs that are hung using net/ssh.
Now what I want to do is use the job ID that is output from the script (3rd column with all the numbers) and cancel the jobs (I know how I'm going to do the cancel) Is there a way, in Ruby, to use outputted digits as input?
Source:
#!/usr/local/bin/ruby
require 'rubygems'
require 'net/ssh'
require 'etc'
require 'adfitech/mail'
class PrintJobs
HOST = ARGV[0]
USERNAME = Etc.getlogin
PASSWORD = nil
def scan_for_jobs
check_jobs = Net::SSH.start(HOST, USERNAME, :password => PASSWORD) do |ssh|
cmd = "prt_jobs"
info = ssh.exec!(cmd)
res = info.split("\n").reject {|line| line.match(/\s+2016\s+/)}.join("\n")
puts res
print "Kill jobs: "
input = STDIN.gets.chomp.upcase
if input == "YES"
kill_jobs(check_jobs, res)
else
exit 1
end
end
end
def kill_jobs(check_jobs, res)
puts "Loading jobs in kill que.."
# <= Here output digits
end
end
test = PrintJobs.new
test.scan_for_jobs
output:
#3rd column with the digits is the job ID
laser26-828837 kaj 1042432 Fri 21 Aug 2015 03:59:35 PM CDT
laser26-828982 leb 446464 Fri 21 Aug 2015 04:52:20 PM CDT
laser26-828983 leb 1042432 Fri 21 Aug 2015 04:52:20 PM CDT
laser26-828986 leb 446464 Fri 21 Aug 2015 05:04:39 PM CDT
laser26-828987 leb 1042432 Fri 21 Aug 2015 05:04:39 PM CDT
laser26-828991 leb 446464 Fri 21 Aug 2015 05:15:08 PM CDT
laser26-828992 leb 1042432 Fri 21 Aug 2015 05:15:08 PM CDT
laser26-898419 kaj 430080 Wed 14 Oct 2015 02:01:34 PM CDT
laser26-898420 kaj 1042432 Wed 14 Oct 2015 02:01:34 PM CDT
laser26-898444 kaj 430080 Wed 14 Oct 2015 02:09:08 PM CDT
laser26-898445 kaj 1042432 Wed 14 Oct 2015 02:09:08 PM CDT
laser26-898526 kaj 446464 Wed 14 Oct 2015 02:50:45 PM CDT
laser26-898527 kaj 1042432 Wed 14 Oct 2015 02:50:45 PM CDT
laser26-898577 kaj 446464 Wed 14 Oct 2015 03:09:03 PM CDT
laser26-898578 kaj 1042432 Wed 14 Oct 2015 03:09:04 PM CDT
laser26-898583 kaj 430080 Wed 14 Oct 2015 03:13:27 PM CDT
laser26-898584 kaj 1042432 Wed 14 Oct 2015 03:13:27 PM CDT
laser26-898587 kaj 446464 Wed 14 Oct 2015 03:17:17 PM CDT
laser26-898588 kaj 1042432 Wed 14 Oct 2015 03:17:17 PM CDT
laser26-898596 kaj 446464 Wed 14 Oct 2015 03:25:40 PM CDT
laser26-898597 kaj 1042432 Wed 14 Oct 2015 03:25:40 PM CDT
laser26-898602 kaj 446464 Wed 14 Oct 2015 03:26:13 PM CDT
laser26-898603 kaj 1042432 Wed 14 Oct 2015 03:26:13 PM CDT
laser26-898617 kaj 430080 Wed 14 Oct 2015 03:31:27 PM CDT
laser26-898618 kaj 1042432 Wed 14 Oct 2015 03:31:27 PM CDT
laser26-903874 bja 446464 Tue 20 Oct 2015 09:18:34 AM CDT
laser26-903875 bja 1042432 Tue 20 Oct 2015 09:18:34 AM CDT
laser26-904044 kaj 430080 Tue 20 Oct 2015 10:11:32 AM CDT
laser26-904045 kaj 1042432 Tue 20 Oct 2015 10:11:32 AM CDT
laser26-904171 kaj 446464 Tue 20 Oct 2015 10:34:45 AM CDT
laser26-904172 kaj 1042432 Tue 20 Oct 2015 10:34:45 AM CDT
laser26-904368 bja 430080 Tue 20 Oct 2015 11:33:48 AM CDT
laser26-904369 bja 1042432 Tue 20 Oct 2015 11:33:48 AM CDT
laser26-904479 bja 446464 Tue 20 Oct 2015 12:06:10 PM CDT
laser26-904480 bja 1042432 Tue 20 Oct 2015 12:06:10 PM CDT
laser26-904495 bja 430080 Tue 20 Oct 2015 12:10:26 PM CDT
laser26-904496 bja 1042432 Tue 20 Oct 2015 12:10:26 PM CDT
laser26-966772 ksn 430080 Thu 03 Dec 2015 03:35:28 PM CST
laser26-966773 ksn 1042432 Thu 03 Dec 2015 03:35:28 PM CST
laser26-966861 ksn 446464 Thu 03 Dec 2015 03:50:00 PM CST
laser26-966862 ksn 1042432 Thu 03 Dec 2015 03:50:00 PM CST
laser26-966979 ksn 446464 Thu 03 Dec 2015 04:18:25 PM CST
laser26-966980 ksn 1042432 Thu 03 Dec 2015 04:18:25 PM CST
laser26-966989 ksn 430080 Thu 03 Dec 2015 04:23:05 PM CST
laser26-966990 ksn 1042432 Thu 03 Dec 2015 04:23:05 PM CST
laser32-829104 glt 187392 Sun 23 Aug 2015 07:22:16 PM CDT
laser35-797457 jss 572416 Wed 29 Jul 2015 02:59:20 PM CDT
laser35-806865 kjw 982016 Wed 05 Aug 2015 02:15:55 PM CDT
laser40-898540 rrw 715776 Wed 14 Oct 2015 02:51:30 PM CDT
laser40-898547 rrw 715776 Wed 14 Oct 2015 02:53:05 PM CDT
laser40-904373 rrw 2565120 Tue 20 Oct 2015 11:37:39 AM CDT
mailrm2-829117 lmb 356352 Mon 24 Aug 2015 06:20:59 AM CDT
pref_jet-797185 djl 871424 Wed 29 Jul 2015 01:46:38 PM CDT
pref_jet-797187 aqs 1104896 Wed 29 Jul 2015 01:46:57 PM CDT
pref_jet-829111 djl 117760 Mon 24 Aug 2015 05:38:54 AM CDT
pref_jet-829112 djl 117760 Mon 24 Aug 2015 05:41:05 AM CDT
team_d-797167 kld 144384 Wed 29 Jul 2015 01:39:29 PM CDT
team_d-797168 kld 145408 Wed 29 Jul 2015 01:39:39 PM CDT
team_d-797169 kld 144384 Wed 29 Jul 2015 01:39:48 PM CDT
team_d-797170 kld 145408 Wed 29 Jul 2015 01:39:59 PM CDT
team_d-797171 kld 144384 Wed 29 Jul 2015 01:40:10 PM CDT
team_d-797172 kld 144384 Wed 29 Jul 2015 01:40:23 PM CDT
team_d-797173 kld 144384 Wed 29 Jul 2015 01:40:31 PM CDT
team_d-797174 kld 144384 Wed 29 Jul 2015 01:40:41 PM CDT
team_d-797175 kld 144384 Wed 29 Jul 2015 01:40:50 PM CDT
team_d-797176 kld 144384 Wed 29 Jul 2015 01:41:00 PM CDT
team_d-797177 kld 144384 Wed 29 Jul 2015 01:41:08 PM CDT
team_d-797178 kld 144384 Wed 29 Jul 2015 01:41:16 PM CDT
team_d-797179 kld 144384 Wed 29 Jul 2015 01:41:25 PM CDT
team_d-797180 kld 145408 Wed 29 Jul 2015 01:41:37 PM CDT
team_d-797181 kld 145408 Wed 29 Jul 2015 01:41:45 PM CDT
team_d-797186 kld 144384 Wed 29 Jul 2015 01:46:40 PM CDT
team_d-829108 rdm 429056 Mon 24 Aug 2015 04:55:43 AM CDT
team_d-850252 kld 145408 Wed 09 Sep 2015 12:34:18 PM CDT
team_d-869247 kld 77824 Wed 23 Sep 2015 07:57:35 AM CDT
team_d-869248 kld 81920 Wed 23 Sep 2015 07:57:43 AM CDT
team_d-898621 kld 39936 Wed 14 Oct 2015 03:32:01 PM CDT
team_d-898622 kld 39936 Wed 14 Oct 2015 03:32:12 PM CDT
team_d-898633 kld 36864 Wed 14 Oct 2015 03:33:59 PM CDT
team_d-904046 kmt 16634880 Tue 20 Oct 2015 10:12:04 AM CDT
team_d-904497 kmt 5965824 Tue 20 Oct 2015 12:11:56 PM CDT
team_f-898550 rlr 1070080 Wed 14 Oct 2015 02:55:30 PM CDT
team_t-795252 tud 891904 Tue 28 Jul 2015 01:40:06 PM CDT
team_t-795255 tud 970752 Tue 28 Jul 2015 01:40:41 PM CDT
team_t-795256 tud 1033216 Tue 28 Jul 2015 01:40:55 PM CDT
team_t-795263 dlc 228352 Tue 28 Jul 2015 01:46:07 PM CDT
team_t-795264 alm 1280000 Tue 28 Jul 2015 01:46:30 PM CDT
team_t-797184 slp 154624 Wed 29 Jul 2015 01:46:25 PM CDT
team_t-797188 slp 155648 Wed 29 Jul 2015 01:47:20 PM CDT
team_t-797456 alm 244736 Wed 29 Jul 2015 02:59:17 PM CDT
team_t-828984 slp 16384 Fri 21 Aug 2015 04:53:01 PM CDT
team_t-828985 slp 16384 Fri 21 Aug 2015 04:56:14 PM CDT
team_t-828993 slp 192512 Fri 21 Aug 2015 05:16:37 PM CDT
team_t-829082 dlc 243712 Sun 23 Aug 2015 07:44:47 AM CDT
team_t-829103 glt 187392 Sun 23 Aug 2015 07:20:19 PM CDT
team_t-829114 ddh 17408 Mon 24 Aug 2015 06:03:36 AM CDT
team_t-829115 ddh 16384 Mon 24 Aug 2015 06:04:16 AM CDT
team_t-898551 alh 219136 Wed 14 Oct 2015 02:56:02 PM CDT
team_t-898552 alh 238592 Wed 14 Oct 2015 02:56:21 PM CDT
team_t-898619 ljr 141312 Wed 14 Oct 2015 03:31:28 PM CDT
team_t-898717 ljr 144384 Wed 14 Oct 2015 03:54:16 PM CDT
team_t-898826 dlc 320512 Thu 15 Oct 2015 06:39:42 AM CDT
team_t-898834 alm 367616 Thu 15 Oct 2015 07:05:41 AM CDT
team_t-966858 ljr 1857536 Thu 03 Dec 2015 03:48:23 PM CST
team_t-966863 ljr 555008 Thu 03 Dec 2015 03:50:10 PM CST
thermal12-980170 clw 1024 Fri 11 Dec 2015 02:32:39 PM CST
thermal13-851959 wdp 1024 Thu 10 Sep 2015 11:35:08 AM CDT
thermal13-898422 wdp 1024 Wed 14 Oct 2015 02:01:37 PM CDT
thermal13-967002 wdp 1024 Thu 03 Dec 2015 04:24:59 PM CST
thermal13-967011 wdp 1024 Thu 03 Dec 2015 04:25:49 PM CST
thermal20-870628 czp 1024 Wed 23 Sep 2015 02:08:13 PM CDT
Kill jobs: yes
Loading jobs in kill que..
[tep#coltrane ruby]$ ruby prt_jobs_check davey
closing_2-205137 snb 231424 Wed 29 Jul 2015 01:40:00 PM CDT
closing_2-205138 snb 232448 Wed 29 Jul 2015 01:40:33 PM CDT
closing_2-205139 jiw 242688 Wed 29 Jul 2015 01:40:48 PM CDT
closing_2-205140 jiw 228352 Wed 29 Jul 2015 01:41:06 PM CDT
closing_2-205164 jiw 222208 Wed 29 Jul 2015 02:38:37 PM CDT
laser20-205141 sdj 814080 Wed 29 Jul 2015 01:45:28 PM CDT
laser20-205142 sdj 649216 Wed 29 Jul 2015 01:45:44 PM CDT
laser20-205143 sdj 649216 Wed 29 Jul 2015 01:47:05 PM CDT
laser27-205163 acm 684032 Wed 29 Jul 2015 02:38:20 PM CDT
laser27-209301 acm 14336 Fri 21 Aug 2015 07:43:38 AM CDT
laser27-220263 acm 265216 Thu 15 Oct 2015 06:54:16 AM CDT
laser27-220264 acm 16384 Thu 15 Oct 2015 06:54:17 AM CDT
laser31-220266 jal 1024 Thu 15 Oct 2015 06:56:57 AM CDT
laser32-220265 tep 39936 Thu 15 Oct 2015 06:54:54 AM CDT
npl_3-220245 jrr 176128 Wed 14 Oct 2015 03:32:03 PM CDT
ship_setup2-205177 slf 1193984 Wed 29 Jul 2015 03:02:27 PM CDT
ship_setup2-205178 slf 1571840 Wed 29 Jul 2015 03:02:47 PM CDT
ship_setup2-205180 slf 1571840 Wed 29 Jul 2015 03:03:34 PM CDT
team_d-209530 akn 1393664 Sun 23 Aug 2015 07:09:12 AM CDT
team_d-209533 akn 50176 Sun 23 Aug 2015 12:47:52 PM CDT
team_d-209534 akn 50176 Sun 23 Aug 2015 01:05:16 PM CDT
team_d-209535 akn 50176 Sun 23 Aug 2015 01:07:02 PM CDT
team_f-206217 jlh 1327104 Wed 05 Aug 2015 01:07:12 PM CDT
verif_jet-220261 twk 1369088 Thu 15 Oct 2015 06:51:34 AM CDT
verif_jet-220262 twk 1363968 Thu 15 Oct 2015 06:52:09 AM CDT
verif_jet-220267 twk 1372160 Thu 15 Oct 2015 07:01:45 AM CDT
verif_jet-220268 twk 1374208 Thu 15 Oct 2015 07:02:00 AM CDT
verif_jet-221015 sum 1258496 Tue 20 Oct 2015 11:44:50 AM CDT
verif_jet-221016 sum 1198080 Tue 20 Oct 2015 11:45:32 AM CDT
verif_jet-221017 sum 1227776 Tue 20 Oct 2015 11:46:34 AM CDT
verif_jet-221018 sum 1190912 Tue 20 Oct 2015 11:47:51 AM CDT
verif_jet-221019 sum 1179648 Tue 20 Oct 2015 11:48:08 AM CDT
verif_jet-221030 tad 1681408 Tue 20 Oct 2015 12:11:39 PM CDT
verif_jet-221031 tad 903168 Tue 20 Oct 2015 12:11:44 PM CDT
verif_jet-221032 tad 872448 Tue 20 Oct 2015 12:11:50 PM CDT
verif_jet-221033 tad 1716224 Tue 20 Oct 2015 12:12:56 PM CDT
Kill jobs: yes
Loading jobs in kill que..
You're question is very vague but I think I get it so I'll take a stab at it. I think you want to parse that output and get the Job IDs, right? I'm assuming the Job ID is in the 3rd column in that output, let's take an excerpt:
output = <<-HEREDOC
laser26-828837 kaj 1042432 Fri 21 Aug 2015 03:59:35 PM CDT
laser26-828982 leb 446464 Fri 21 Aug 2015 04:52:20 PM CDT
laser26-828983 leb 1042432 Fri 21 Aug 2015 04:52:20 PM CDT
HEREDOC
To get all the Job IDS from that string, we can map over each line, split the line up by spaces and get the 3rd item:
column = 2 # 3rd column
job_ids = output.lines.map { |line| line.split(/\s+/)[2] }
# => ["1042432", "446464", "1042432"]
Hope that helps.
I have three shell scripts say A, B and C. I need to run A in the background and run B in the background till A finishes its execution in the background. Similarly run C in the foreground till A and B finish their execution.
I was doing this for 2 processes earlier in this way.
./A.sh &
while ps -p $! >/dev/null; do
./B.sh
done
I need to run B in background and C in foreground till A finishes its execution in background. How do I modify the above code.
This will run A & B in the background with C in the foreground; B&C will loop until A finishes:
#!/bin/bash
./A.sh &
APID=$!
while ps -p ${APID} >/dev/null; do
./B.sh & ./C.sh
done
Example from my box:
[ 09:39 jon#hozbox.com ~/SO/bash ]$ cat A.sh
#!/bin/bash
echo "A Started at: `date`"
sleep 30
echo "A Finished at: `date`"
[ 09:39 jon#hozbox.com ~/SO/bash ]$ cat B.sh
#!/bin/bash
echo "B Started at: `date`"
sleep 10
echo "B Finished at: `date`"
[ 09:39 jon#hozbox.com ~/SO/bash ]$ cat C.sh
#!/bin/bash
echo "C Started at: `date`"
sleep 5
echo "C Finished at: `date`"
[ 09:38 jon#hozbox.com ~/SO/bash ]$ ./how-to-program-in-this-bash-script-of-background-processes.sh
A Started at: Wed Nov 23 09:38:39 PST 2011
C Started at: Wed Nov 23 09:38:39 PST 2011
B Started at: Wed Nov 23 09:38:39 PST 2011
C Finished at: Wed Nov 23 09:38:44 PST 2011
C Started at: Wed Nov 23 09:38:44 PST 2011
B Started at: Wed Nov 23 09:38:44 PST 2011
B Finished at: Wed Nov 23 09:38:49 PST 2011
C Finished at: Wed Nov 23 09:38:49 PST 2011
C Started at: Wed Nov 23 09:38:49 PST 2011
B Started at: Wed Nov 23 09:38:49 PST 2011
B Finished at: Wed Nov 23 09:38:54 PST 2011
C Finished at: Wed Nov 23 09:38:54 PST 2011
C Started at: Wed Nov 23 09:38:54 PST 2011
B Started at: Wed Nov 23 09:38:54 PST 2011
B Finished at: Wed Nov 23 09:38:59 PST 2011
C Finished at: Wed Nov 23 09:38:59 PST 2011
C Started at: Wed Nov 23 09:38:59 PST 2011
B Started at: Wed Nov 23 09:38:59 PST 2011
B Finished at: Wed Nov 23 09:39:04 PST 2011
C Finished at: Wed Nov 23 09:39:04 PST 2011
C Started at: Wed Nov 23 09:39:04 PST 2011
B Started at: Wed Nov 23 09:39:04 PST 2011
A Finished at: Wed Nov 23 09:39:09 PST 2011
B Finished at: Wed Nov 23 09:39:09 PST 2011
C Finished at: Wed Nov 23 09:39:09 PST 2011
[ 09:39 jon#hozbox.com ~/SO/bash ]$
What about forking A and B at the same time, but put A second so $! gives A's pid:
./B.sh & ./A.sh &
while ps -p $! >/dev/null; do
./C.sh
done
Heres an example from my box:
[ 19:08 jon#hozbox.com ~ ]$ date
Tue Nov 22 19:08:19 PST 2011
[ 19:08 jon#hozbox.com ~ ]$ sleep 15 & sleep 20 &
[1] 1126
[2] 1127
[ 19:08 jon#hozbox.com ~ ]$ while ps -p $! > /dev/null; do sleep 1 && date; done
Tue Nov 22 19:08:26 PST 2011
Tue Nov 22 19:08:27 PST 2011
Tue Nov 22 19:08:28 PST 2011
Tue Nov 22 19:08:29 PST 2011
Tue Nov 22 19:08:31 PST 2011
Tue Nov 22 19:08:32 PST 2011
Tue Nov 22 19:08:33 PST 2011
Tue Nov 22 19:08:34 PST 2011
Tue Nov 22 19:08:35 PST 2011
Tue Nov 22 19:08:36 PST 2011
Tue Nov 22 19:08:37 PST 2011
Tue Nov 22 19:08:38 PST 2011
[1]- Done sleep 15
Tue Nov 22 19:08:39 PST 2011
Tue Nov 22 19:08:40 PST 2011
Tue Nov 22 19:08:41 PST 2011
Tue Nov 22 19:08:42 PST 2011
Tue Nov 22 19:08:43 PST 2011
[2]+ Done sleep 20
Tue Nov 22 19:08:44 PST 2011
[ 19:08 jon#hozbox.com ~ ]$
From your picture, it looks like you want B and C to loop while A is running, and then kill both after A completes. Here is the code for that:
# Run A in the background and get its process ID
./A.sh &
PIDA = $!
# Loop B in the background and get its process ID
while ps $PIDA >/dev/null 2>&1; do
./B.sh
done &
PIDB = $!
# Loop C like B, get its PID
while ps $PIDA >/dev/null 2>&1; do
./C.sh
done &
$PIDC = $!
# Wait until A finishes, then kill B and C
wait $PIDA
ps $PIDB >/dev/null 2>&1 && kill $PIDB
ps $PIDC >/dev/null 2>&1 && kill $PIDC