BASH Fun: Running a command at a specific time using 'at'. 
[acool@acool2 ~]$ sudo yum install at
...
[acool@acool2 ~]$ sudo systemctl start atd
[acool@acool2 ~]$ sudo systemctl enable atd


[acool@acool2 ~]$ date
Mon Apr 15 16:09:15 PDT 2019
[acool@acool2 ~]$
[acool@acool2 ~]$ echo 'date >> at-fun.txt' | at now + 1 minute
job 79 at Mon Apr 15 16:10:00 2019
[acool@acool2 ~]$
[acool@acool2 ~]$ atq
79 Mon Apr 15 16:10:00 2019 a acool
[acool@acool2 ~]$
[acool@acool2 ~]$ cat at-fun.txt
Mon Apr 15 16:10:00 PDT 2019


[acool@acool2 ~]$
[acool@acool2 ~]$ cat at-fun.txt
[acool@acool2 ~]$ echo 'php -v >> at-fun.txt' | at 4:16 PM April 15 2019
job 84 at Mon Apr 15 16:16:00 2019
[acool@acool2 ~]$
[acool@acool2 ~]$ date
Mon Apr 15 16:15:21 PDT 2019
[acool@acool2 ~]$ atq
84 Mon Apr 15 16:16:00 2019 a acool
[acool@acool2 ~]$
[acool@acool2 ~]$ cat at-fun.txt
PHP 7.0.27 (cli) (built: Apr 4 2018 13:48:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
[acool@acool2 ~]$

https://stackoverflow.com/questions/55524769

[ view entry ] ( 777 views )   |  print article
Memcached: Looping through a set of servers to delete a key. 
Cool way to make sure a key does not exist in a group of memcached servers. Handy when troubleshooting or when cached data needs a purge. Tested:
[aesteban@localhost ~]$ 
[aesteban@localhost ~]$ for i in {1..6};do ( echo 'delete events.slick-range') | nc server-${i}.example.com 11211; done;
NOT_FOUND
NOT_FOUND
NOT_FOUND
DELETED
NOT_FOUND
NOT_FOUND
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$

Try adding a delay if the above doesn't seem to work:
[aesteban@localhost ~]$ 
[aesteban@localhost ~]$ for i in {1..6};do ( sleep 2; echo 'delete events.slick-range') | nc -d 2 server-${i}.example.com 11211; done;
NOT_FOUND
NOT_FOUND
NOT_FOUND
DELETED
NOT_FOUND
NOT_FOUND
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$




BONUS (6/12/2019) - Find out the expiration date for a key:
(sleep 1; echo "stats cachedump 7 0"; sleep 1; echo "quit";) | nc www-server01.example.com  11211 | grep 'memc.sess.tpnm0jd8aottlturb0ml510sd0'


Increase/decrease the slab (seven) number as need until the key is found.

[ view entry ] ( 743 views )   |  print article
BASH: Looping through all lines in a file 
Content of getThem.sh :
cd logos;

while read line; do

# test if line starts with "assets"
if [[ $line =~ ^asset.* ]]; then
#prepend domain
line="http://www.soy-example.com/$line"
fi

# test if line starts with "/assets"
if [[ $line =~ ^/asset.* ]]; then
# prepend domain
line="http://www.soy-example.com$line"
fi

# download this sucker
wget $line

done < ../soyFranquiciasLogos.txt


[ view entry ] ( 1200 views )   |  print article
BASH 101: Dollar sign madness 
positional parameters, parameters passed to script.
$1, $2, $3, ... 

It's an array-like construct of all positional parameters, {$1, $2, $3 ...}.
"$@" 

It's the IFS expansion of all positional parameters, $1 $2 $3 ....
"$*" 

It's the number of positional parameters.
$# 

current options set for the shell.
$- 

pid of the current shell (not subshell).
$$ 

most recent parameter (or the abs path of the command to start the current shell immediately after startup).
$_ 

It's the (input) field separator.
$IFS 

It's the most recent foreground pipeline exit status.
$? 

It's the PID of the most recent background command.
$!

It's the name of the shell or shell script.
$0


http://stackoverflow.com/questions/5163 ... -variables

[ view entry ] ( 1217 views )   |  print article
BASH 101: Functions 
EXAMPLE A) Declaring a function in sum.sh script:
#!/usr/bin/bash

sum()
{
echo $1 + $2 | bc
}

# call function and pass cli
# parameters to function
sum $1 $2

Calling script:
[aesteban@localhost ~]$ ./sum.sh 1.4 1.3
2.7

EXAMPLE B) A different way to write sum.sh script:
#!/usr/bin/bash

sum()
{
RESULT=$(echo $1+$2 | bc) # or: RESULT=`echo $1+$2 | bc`
}

sum $1 $2

echo $RESULT

Calling modified script:
[aesteban@localhost ~]$ ./sum.sh 1.7 1.2
2.9

EXAMPLE C) Another way to write the same crap:
#!/usr/bin/bash

sum()
{
echo $1+$2 | bc
}

SUMRESULT=`sum $1 $2`

echo $SUMRESULT

Calling it:
[aesteban@localhost ~]$ ./sum.sh 2.9 3
5.9


[ view entry ] ( 1263 views )   |  print article
BASH 101: Notes 
#local variables
[aesteban@localhost ~]$ Name=Angel
[aesteban@localhost ~]$ echo $Name
Angel
[aesteban@localhost ~]$ Name="Angel Cool"
[aesteban@localhost ~]$ echo $Name
Angel Cool
[aesteban@localhost ~]$ #readonly
[aesteban@localhost ~]$ readonly Name
[aesteban@localhost ~]$ Name="Angel Esteban"
bash: Name: readonly variable
[aesteban@localhost ~]$

#index arrays
[aesteban@localhost ~]$ Fruits[0]=apple
[aesteban@localhost ~]$ Fruits[1]=orange
[aesteban@localhost ~]$ Fruits[2]=banana
[aesteban@localhost ~]$ Fruits[3]=grapes
[aesteban@localhost ~]$
[aesteban@localhost ~]$ States=("California" "Texas" "Florida")
[aesteban@localhost ~]$ echo ${States[1]}
Texas

[aesteban@localhost ~]$ echo ${Fruits[2]}
banana
[aesteban@localhost ~]$ echo ${Fruits[*]}
apple orange banana grapes
[aesteban@localhost ~]$ echo ${#Fruits[*]}
4
[aesteban@localhost ~]$ unset Fruits
[aesteban@localhost ~]$ echo ${!States[*]}
0 1 2
[aesteban@localhost ~]$

#associative arrays
[aesteban@localhost ~]$ declare -A INFO
[aesteban@localhost ~]$
[aesteban@localhost ~]$ INFO["name"]="Angel Esteban Cool"
[aesteban@localhost ~]$ echo ${INFO["name"]}
Angel Esteban Cool
[aesteban@localhost ~]$
[aesteban@localhost ~]$ INFO=([name]="Angel Cool" [website]="angelcool.net")
[aesteban@localhost ~]$
[aesteban@localhost ~]$ echo ${INFO["website"]}
angelcool.net
[aesteban@localhost ~]$


Loops
[aesteban@localhost ~]$ for i in $(seq 1 5); do echo This line corresponds to line $i.;done;
This line corresponds to line 1.
This line corresponds to line 2.
This line corresponds to line 3.
This line corresponds to line 4.
This line corresponds to line 5.
[aesteban@localhost ~]$
[aesteban@localhost ~]$
[aesteban@localhost ~]$ for i in {1..7}; do echo This is number $i.;done;
This is number 1.
This is number 2.
This is number 3.
This is number 4.
This is number 5.
This is number 6.
This is number 7.
[aesteban@localhost ~]$
[aesteban@localhost var]$ for i in `ls /var`;do echo $i;done;
...
[aesteban@localhost ~]$# looping through a list of servers
[aesteban@localhost ~]$for i in {1..5}; do ssh www-app0${i} "cd /var/www/html/barney.com && git pull origin live" ;done;


[ view entry ] ( 1321 views )   |  print article

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |


2024 By Angel Cool