Automating Your Daily Tasks with Scripting Adler Hsieh RubyConf Taiwan 2015-9-11
Automating Your Daily Tasks with Scripting
Adler Hsieh
RubyConf Taiwan
2015-9-11
Agenda
• What is Scripting
• Why Scripting
• Several Cases
• Script Management
• Q & A
Only Basic Concept
Who am I
• Adler Hsieh
• Software Engineer at OneAD
• Ruby, JavaScript, PHP
Github: adlerhsieh
Twitter: @adler_hsieh
Blog: http://motion-express.com
What is Scripting
Let’s make it simple
Um…
Let’s make it a little useful
How do I get my local ip address?
> ifconfig
What is scripting
192.168.1.149
Your local ip address
What is scripting
What is scripting
Basically…
Small programs
solving
Small problems
Plus…
• Saves time
• Easy to write
• Easy to manage
And…
Show off
Cases
• Cleaning old files
• Shutdown local server
• MySQL Database import from .sql
Cleaning Old Files
Scenario
• All downloaded files are in one directory
• Directory becomes very large
Cleaning Old Files
Cleaning Old Files
Cleaning Old Files
Use it with Cron
Shutdown Server
Scenario
• Local server crashes
• I cannot shut down my rails server with ctrl + c
Shutdown Server
> lsof –i:3000
> kill -9 PID
Shutdown Server
Importing MySQL database with .sql
Scenario
• When there is bug
• Importing production DB to local DB
Importing MySQL database with .sql
> ssh [email protected]
> mysqldump –u username –p db_name > db.sql
> exit
> scp [email protected]:db.sql db.sql
> mysql –u username –p db_name < db.sql
Importing MySQL database with .sql
Cases
• Scripts are task-specific
• Find patterns in your tasks
Managing Scripts
Managing Scripts
If you do not manage scripts…
• Scripts everywhere
• You have to remember file locations
Managing Scripts
Purpose
• All scripts at a glance
• Write once, run everywhere
Rake
Managing Scripts
• Rake
– Managing all rake tasks in ~/.rake folder
task_a.rake
task_b.rake
– Prefixing ‘rake’ command
– Description support
Managing Scripts
Managing Scripts
• Managing tasks like in Rails
– restart.rake
– import.rake
– local_ip.rake
– clean.rake
Thus, run `rake` command from anywhere(add –g option if necessary)
Executables
Managing Scripts
• Make scripts executables
– Separate scripts by commands
• The bad part
– Too many commands
– Conflicting with existing commands
Managing Scripts
> chmod 755 hello.rb
> mv hello.rb hello
> ./hello
Managing Scripts
• Change the following line in your ‘.bashrc’:
export PATH=“/user/bin:/usr/local/bin”
• To:
export PATH=“/user/bin:/usr/local/bin:/scripts”
• And you can do:
> hello
That’s it!
Recap
• Scripts are small programs solving small problems
• Scripts are case-specific
• Automation saves energy & time
• Managing scripts with rake
Make your own scripts!
Thank you!Slides are available on my Twitter
@adler_hsieh