Is there a way to record the Linux commands I use and reuse them on another machine or in case I want to reinstall the machine?
In example record how I set up NginX, PHP and permissions in my lab environment and then repeat the exact commands on my deploy machine?
Seems to me like you want script(1).
It allows you to record and replay terminal sessions.
On the other hand, if you are trying to automate setups, consider configuration management, like puppet.
Two ways I can think of off the top of my head:
Use the builtin command history
Do everything in a screen session and enable logging
Bash records commands in a file called ~/.bash_history. This works if you do everything via a command line, and in one command window.
Be warned though, it limits the size of that file to the most recent 1000(by default) commands.
try this out
script /tmp/it356658-$(uname -n).out
# date
I think you could also use autoexpect for this task.
Normally I use a command like
history > ~/tmp/hh
to save a history file. Then I usevi
, jump to the bottom with the G command and then search upwards until I find the part that would be near a useful entry point into a script. From there I collapse out extraneousls
commands, combine series of incrementalcd
into directories, add comments, and start figuring out which parts need to be arguments, which things might have to be environment settings and which things have to be hard-coded. I add lots of comments. Sometimes I "punt" on parts of automating it and just give the user some instructions (go find this setting,ssh
to foo and ..., uncomment blah from this file and move it to blarg) orecho
a URL ... and a prompt string like: 'Configured Foo and exit >'(When you punt on an automated file edit ... I suggest appending the instructive code to the file, as a comment, and use something like the
vi
+ command line switch to put the editor's cursor near the bottom of the file, at the top of your instructions. For example with 5 lines of instructive text (a here doc for example) you'd add'+$-5'
to thevi
command line, before the filename. ($-5
is an oldex
incantation to address the last line of the file, minus five lines; the relatively obscure+
switch tovi
accepts anyex
... : command ... as an argument).Another trick ... assuming you do most of your work from a X Windowing System terminal (
xterm
,konsole
,gnome-terminal
, etc) then keep a browser open on some wiki pages as you go. Cut and past commands, output, snippets of text files and jot down notes as you go. (This mostly assumes you're mostly working on your servers overssh
from your workstation, desktop, laptop, or whatever).Save early and often.
Create new wiki pages for different processes or stages. Then link them together and link out to any references out on the 'net. (You know, all those Google pages you used every time you got stuck at any point).
Some of my best HOWTO documents have been created this way.
If you do it right then you might be able to get the job off your plate the old-fashioned way, by delegating to the junior team member ... or the eager intern. :)