Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webapp update shell script

I feel silly asking this...

I am not an expert on shell scripting, but I am finally in enough of a sysadmin role that I want to do this correctly.

I have a production server that hosts a webapp. Here is my routine.
1 - ssh to server
2 - cd django_src/django_apps/team_proj
3 - svn update
4 - sudo /etc/init.d/apache2 restart
5 - logout

I want to create a shell script for steps 2,3,4.

I can do this, but it will be a very plain and simple bash script simply containing the actual commands I type at the command line.

My question: What is the best way to script this kind of repetitive procedure in bash (Linux, Ubuntu) for a remote server?

Thanks!

like image 979
Art Avatar asked May 29 '26 08:05

Art


2 Answers

The best way is simply as you suggest. Some things you should do for your script would be:

  • put set -e at the top of the script (after the shebang). This will cause your script to stop if any of the commands fail. So if it cannot cd to the directory, it will not run svn update or restart apache. You can do this programmatically by putting || exit 0 after each command, but if that's all you're doing, you may as well use set -e
  • Use full paths in your script. Do not assume the directory that the script is run from. In this specific case, the cd command has a relative path. Use a full (absolute) path, or use an environment variable like $HOME.
  • You may want to set up sudo so that it can run the command without asking for a password. This makes your script non-interactive which means it can be run in the background and from cron jobs and such.

As time goes by, you may add features and take command line arguments to parameterise the script. But don't bother doing this up front. Just evolve your scripts as you need.

like image 159
camh Avatar answered Jun 01 '26 00:06

camh


There is nothing wrong with a simple bash script simply containing the actual commands you type at the command line. Don't make it more complicated than necessary.

like image 39
Erich Kitzmueller Avatar answered May 31 '26 23:05

Erich Kitzmueller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!