8

I would like to install some software on a linux-machine that I have run in VirtualBox. Then I would like to do the same thing on a linux-VPS.

I think that I can save all commands that I run using the history command. Is there any way I could run these commands on another machine? Or what is the way to do such things?

Jonas
  • 1,593

6 Answers6

5

There are several tools out there that allow you to log in to and execute series of commands on multiple machines at the same time. Here are a couple:

Caleb
  • 70,105
5

Well, I imagine 2 situations here:

  1. If I need to run a small number of commands I would just run them again on any other machine (by small number I mean less then 10)

  2. If I need to run many commands I would put them into a bash script and run the script on all the other machines. The script should look like this:

    #!/bin/bash
    command 1
    command 2
    command 3
    command 4
    

If you are unsure about the outcome of some commands, you can separate the commands by &&, meaning that the next command will execute only if the previous was successful. command1 && command2 means that command2 will not be executed if command1 fails.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
Patkos Csaba
  • 2,530
  • Thanks for a great answer. I will set up PHP+Nginx+PostgreSQL so I guess the script solution is the way to go. – Jonas Oct 18 '10 at 10:23
  • Depends on how much you already know how to do and how many times you plan to duplicate this process. (Although it isn't automated, the most thorough approach for a one or two time thing is probably to keep a big document including working code snippets and thoughts and notes about the setup procedure that you can refer to later...) Once you know how everything works, then you should definitely write a big Bash or Puppet script or whatever. – Kevin Cantu Oct 18 '10 at 20:22
3

Patkos is right, probably it is best to create a script. However, for this, sometimes you have to experiment around till you get it right and need some log of what you did and what was the outcome. Here the tool script comes in handy.

It creates a record of all activity in the terminal it is running in:

Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

fschmitt
  • 8,790
  • Thanks, I will learn more about that. It looks interesting. – Jonas Oct 18 '10 at 11:38
  • how about doing everything on one machine, then check the history file (such as ~/.bash_history for bash, or ~/.histfile for zsh) and maybe copying some over? – phunehehe Oct 18 '10 at 12:03
  • The history is probably truncated to the last 100 commands or some other arbitrary size limit, so you'd have to check into that. – Kevin Cantu Oct 18 '10 at 20:18
2

I built an open-source tool called Overcast to make this sort of thing easier.

You can spin up Virtualbox machines locally (uses Vagrant under the hood) or on a bunch of different cloud providers, and then run any commands or script files across all of them:

# Spin up Ubuntu 14.04 instances on Virtualbox, DigitalOcean, and Linode:
overcast virtualbox create vm.01
overcast digitalocean create vm.02
overcast linode create vm.03
# Run an arbitrary sequence of commands and scripts across all of them:
overcast run vm.* uptime "free -m" ./path/to/my-script.sh
2

http://docs.fabfile.org/0.9.2/ <--- Nice replacement to scripts as it can take action if an step fails

1

If you want to first run the commands interactively on one machine, and then have a script to run them on others, just open your .bash_history (or whatever your shell uses) in a text editor, and remove whatever mistakes you made the first time around. Voila! You've got a script that you can copy to the other machines (with scp, rsync, whatever).

In some cases you may have to process the file slightly first. For instance, I use zsh, and have it configured to add various metadata (datestamps). Obviously you'd need to remove this. You can easily do this in vim or TextMate, or probably any decent editor using block selection. Or you can use awk or cut to output everything but the metadata. Here's one way to do this:

cat .zhistory | cut -d';' -f2- 

But most of the time you'll find your history file is just a straight list of commands, so you probably won't need to worry about any of this massaging.

iconoclast
  • 9,198
  • 13
  • 57
  • 97