July 01, 2006
Unix Automatic Scripts
After writing about Synergy, I realized that I am going to need to learn some UNIX scripts to automate the reloading of the synergy server.
After checking out some resources, I stumbled upon “UNIX Bourne Shell Scripting” that really helped me to understand the programming behind it.
The following command executed the automated kill process of the server
ps a | awk ‘/Synergy/{print $1}’ | xargs kill
I will walk you through section by section to understand what each does.
- get_pid= will assign the variable get_pid to whatever the value is from what follows
- The ps lists the processes in order of their PID (Process ID)
- “a” is a parameter of ps. It selects all processes except session leaders (see getsid(2)) and processes not associated with a terminal.
- | (pipe) is to seperate each additional argument
- “awk” is a programming language designed to search for, match patterns, and perform actions on files.
- You pass awk a Pattern and an Action. The pattern in this case is Synergy (/Synergy/) and the action is to print the 1st column in the output. The first column is the PID which will be used to kill the process.
- kill -9 $get_pid is the command I ran to kill the process IDs that it found
The Following is the entire file called “Synergy.sh” in which you would use the command “sh Synergy.sh” to run it command line. I changed the command a little to run better in the loop.
#!/bin/bash
get_pid=`ps ax | awk ‘/Synergy/{print $1}’`
kill -9 $get_pid
cd /Applications/Synergy
/Applications/Synergy/synergys –config synergy.conf
UPDATED: Found some bugs in the code and I changed it to the current shown above
