«

»

Jul 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

apple, awk, command line, grep, kill, kill processes, ps, scripting, scripts, synergy, terminal, unix

About the author

Timothy Haroutunian

Timothy Haroutunian is a ServiceNow Cloud Implementation Specialist at Acorio. ServiceNow is an IT Management solution that allows for a complete view of your IT and physical environment.

Permanent link to this article: http://www.armenianeagle.com/2006/07/01/unix-automatic-scripts/

http://pagead2.googlesyndication.com/pagead/show_ads.js

2 comments

  1. Zach

    I don’t understand the purpose of your for loop.

  2. Timothy Haroutunian

    I completely agree and after looking it over more, I updated the code with the new one. Thank you for your comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.