I have an infinite loop which uses aws cli to get the microservice names, it's parameters like desired tasks,number of running task etc for an environment.
There are 100's of microservices running in an environment. I have a requirement to compare the value of aws ecs metric running task for a particular microservice in the current loop and with that of the previous loop.
Say name a microservice X has the metric running task 5. As it is an infinite loop, after some time, again the loop come for the microservice X. Now, let's assume the value of running task is 4. I want to compare the running task for currnet loop, which is 4 with the value of the running task for the previous run, which is 5.
If you are asking a generic question of how to keep a previous value around so it can be compared to the current value, just store it in a variable. You can use the following as a starting point:
#!/bin/bash
previousValue=0
while read v; do
echo "Previous value=${previousValue}; Current value=${v}"
previousValue=${v}
done
exit 0
If the above script is called testval.sh. And you have an input file called test.in with the following values:
2
1
4
6
3
0
5
Then running
./testval.sh <test.in
will generate the following output:
Previous value=0; Current value=2
Previous value=2; Current value=1
Previous value=1; Current value=4
Previous value=4; Current value=6
Previous value=6; Current value=3
Previous value=3; Current value=0
Previous value=0; Current value=5
If the skeleton script works for you, feel free to modify it for however you need to do comparisons.
Hope this helps.
I dont know how your input looks exactly, but something like this might be useful for you :
The script
#!/bin/bash
declare -A app_stats
while read app tasks
do
if [[ ${app_stats[$app]} -ne $tasks && ! -z ${app_stats[$app]} ]]
then
echo "Number of tasks for $app has changed from ${app_stats[$app]} to $tasks"
app_stats[$app]=$tasks
else
app_stats[$app]=$tasks
fi
done <<< "$( cat input.txt)"
The input
App1 2
App2 5
App3 6
App1 6
The output
Number of tasks for App1 has changed from 2 to 6
Regards!
35 are the runlevels for the script start
99 and 05 is not understandable
Could some one explain the chkconfig parameters?
Run levels are from 0 to 6.
How this 99 parameter will work when used in the script?
As the man page for chkconfig says,
Each service which should be manageable by chkconfig needs two or more
commented lines added to its init.d script. The first line tells
chkconfig what runlevels the service should be started in by default,
as well as the start and stop priority levels. If the service should
not, by default, be started in any runlevels, a - should be used in
place of the runlevels list. The second line contains a description
for the service, and may be extended across multiple lines with
backslash continuation.
For example, random.init has these three lines:
# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
# higher quality random number generation.
This says that the random script should be started in levels 2, 3, 4, and 5, that its
start priority should be 20, and that its stop priority should be 80.
Start and Stop priorities are used to determine what order initscripts are run in: when starting, lower numbers are run first, when stopping, higher numbers are shutdown first.
I have a file that is created by an application. The app is perpetually writing large volumes of data to the file.
I need to capture a copy of the last 10 lines of the file periodically and write it to another file without causing any file locking, which could impact the application writing the logFile.
How do I do that without impacting the application writing the logfile? i.e without causing any file locking.
logFile example
1452431219885,546,Item Details Request,200,OK,text,true,12562,91,91,271,ip-172-31-36-138,0,134
1452431219886,1300,Select Item Request,200,OK,text,true,28541,91,91,444,ip-172-31-36-138,0,209
1452431219889,210,Login Success Page Request,200,OK,text,true,29405,91,91,123,ip-172-31-36-137,0,27
1452431219898,217,Item Details Request,200,OK,text,true,10620,91,91,215,ip-172-31-36-135,0,106
1452431219900,1668,Logout and Exit Request,200,OK,text,true,19676,92,92,1133,ip-172-31-36-136,0,266
1452431219902,589,Search Page Request,200,OK,text,true,13392,91,91,296,ip-172-31-36-138,0,147
1452431219903,589,Save Basket Request,200,OK,text,true,17473,91,91,294,ip-172-31-36-138,0,147
1452431219908,1135,Search Results Request,200,OK,text,true,561615,91,91,229,ip-172-31-36-136,0,116
1452431219914,1114,Item Details Request,200,OK,text,true,93243,91,91,282,ip-172-31-36-138,0,138
1452431219921,825,Select Item Request,200,OK,text,true,24354,91,91,339,ip-172-31-36-135,0,161
Under most operating systems (i.e. BSDs, Linux, Mac, essentially anything not Windows), files never get locked unless the program explicitly requests it.
Even then, locks in these operating systems are only advisory, so won't affect programs which don't care about them.
So the answer is: almost anything that can get the last lines of the log, will do so without locking.
Obtain the last 10 lines
If I understand your question correctly, you can simply use:
# tail reads the input file and print out the last 10 lines (default)
tail /path/to/file.log > 10_lines.dump
# Change number of lines from default(10) to 5
tail -n 5 /path/to/file.log > 5_lines.dump
Show last lines every X seconds
If you need to do this periodically without saving the output you can use watch:
# watch show the output of the tail command every 5 seconds (default 2s)
watch -n 5 tail /path/to/file.log
# OR last 5 lines every 5 seconds
watch -n 5 tail -n 5 /path/to/file.log
Extract and save for future reference in background
Create a crontab file (name it file.crontab or as you wish) with an editor of your choice with this content:
# ** Crontab file schema **
# .------------------ [m]inute: [0 - 59] OR */x (every x minutes)
# | .-------------- [h]our: [0 - 23] OR */x (every x hours)
# | | .---------- [d]ay [o]f [m]onth: [1 - 31]
# | | | .------ [mon]th: [1 - 12] OR jan,feb,mar,apr...
# | | | | .-- [d]ay [o]f [w]eek: [0 - 6] (Sunday can be 0 or 7)
# | | | | | (also: sun,mon,tue,wed,thu,fri,sat)
# m h d mon dow
# * * * * * command executed every minute with my user permission
# --
# Execute tail every 5 minutes and append last 10 lines of input to log.extract
*/5 * * * * tail "/path/to/file.log" >> "/path/to/log.extract"
Install it on your system using crontab:
crontab file.crontab
I have a simple script that I need to run every 15 minutes everyday (until I get to the last record in my database) giving it greater argument. I know how to do this with the constant argument - example:
*/15 * * * * ./my_awesome_script 1
But I need this, let's say, we start from 8:00 AM:
at 8:00 it should run ./my_awesome_script 1
at 8:15 it should run ./my_awesome_script 2
at 8:30 it should run ./my_awesome_script 3
at 8:45 it should run ./my_awesome_script 4
at 9:00 it should run ./my_awesome_script 5
...
How to make something like this?
I came up with temporary solution:
#!/bin/bash
start=$1
stop=$2
for i in `seq $start $stop`
do
./my_awesome_script $i
sleep 900
done
Writing a wrapper script is pretty much necessary (for sanity's sake). The script can record in a file the previous value of the number and increment it and record the new value ready for next time. Then you don't need the loop. How are you going to tell when you've reached the end of the data in the database? You need to know about how you want to handle that, too.
New cron entry:
*/15 * * * * ./wrap_my_awesome_script
And wrap_my_awesome_script might be:
crondir="$HOME/cron"
counter="$crondir/my_awesome_script.counter"
[ -d "$crondir" ] || mkdir -p "$crondir"
[ -s "$counter" ] || echo 0 > "$counter"
count=$(<"$counter")
((count++))
echo "$count" > $counter
"$HOME/bin/my_awesome_script" "$count"
I'm not sure why you use ./my_awesome_script; it likely means your script is in your $HOME directory. I'd keep it in $HOME/bin and use that name in the wrapper script — as shown.
Note the general insistence on putting material in some sub-directory of $HOME rather than directly in $HOME. Keeping your home directory uncluttered is generally a good idea. You can place the files and programs where you like, of course, but I recommend being as organized as possible. If you aren't organized then, in a few years time, you'll wish you had been.
Basically, I have a series of commands I want to run every other sunday. I set a cron task to run the script every sunday, then this script only allows the script to run on even weeks, thus it only runs every other sunday. My question is, will this script still work going from year to year.
if [ $(($(date +'%U') % 2)) -eq 0 ]
then
some command
fi
You have what's known as the XY problem here.
You have a problem with this part of your shell script, and you want to solve the problem by fixing the script. In reality, fixing the root cause of the problem is easier.
Simply alter your cron job to run every other Sunday:
#----+-----+-----+-----+-----+-------------------------------------------------
#min |hour |day |month|day |command
# | |of mn| |of wk|
#----+-----+-----+-----+-----+-------------------------------------------------
03 04 * * 7 expr `date +%W` % 2 >/dev/null || fortnightly.sh
See How to instruct cron to execute a job every second week? for more info.
If you don't want to specify this with cron syntax, you can use the %s format instead of %U. This will give you the number of seconds since 1st Jan 1970 UTC. You can divide this to get a week number:
$(($(date +'%s') / 604800))
Then you can do your modulo test on that.
Note the number 604800 = 7 * 86400 = 7 * 24 * 60 * 60 ie the number of seconds in one week.
If you're running this every day, you'll want to know that it's actually a Sunday. So in this case, you would divide by 86400 to get a day number. Then, armed with the knowledge that day zero was a Thursday, you can check that the result (modulo 14) is either 3 or 10, depending on which Sunday you started at.