How to start a Rails server with an .sh script in a directory of choice - ruby-on-rails-3

I've been developing with rails for a year now, and although rails is pretty well automated, I'd like to take it one step further.
Every time i start working on a project, i open the terminal, cd to a project folder, than new tab in the terminal, then start the server with "rails s" then back to the first tab to run "sublime ." so that i load the folder in my text editor, Sublime Text.
What i would like is to create a .sh script that would do all that for me, but so far i haven't been able to make it start the server.
So, how to start rails server with a .sh script in a directory of choice?

#Manolo gave me an idea, so I've come up with this:
I modified my .bashrc as the following answer illustrates:
https://superuser.com/a/198022
Basically i added
eval "$BASH_POST_RC"
At the end of my .bashrc so i could run arbitrary commands after it was executed.
Next, i made a following script:
#launch_project.sh
#/bin/bash
cd <PROJECT DIR GOES HERE>;
firefox -P "development";
sublime . &;
gnome-terminal \
--tab --title="Server" -e 'bash -c "export BASH_POST_RC=\"rails s\";exec bash"' \
--tab -e 'bash -c "export BASH_POST_RC=\"git s\"; exec bash"';
That launches my development profile on firefox, my editor with a project root, and terminal window with two tabs - one that runs a WEBrick server, and another one which runs git status for me.
I made a desktop shortcut to the script so i have my own custom IDE of sorts :)

Try this:
#!/bin/sh
cd your_project_folder
nohup rails s > /tmp/rails_server.log 2>&1 &
sublime .
you can see the ouput of your rails server at the /tmp/rails_server.log file

Related

Running .sh scripts in Git Bash

I'm on a Windows machine using Git 2.7.2.windows.1 with MinGW 64.
I have a script in C:/path/to/scripts/myScript.sh.
How do I execute this script from my Git Bash instance?
It was possible to add it to the .bashrc file and then just execute the entire bashrc file.
But I want to add the script to a separate file and execute it from there.
Let's say you have a script script.sh. To run it (using Git Bash), you do the following: [a] Add a "sh-bang" line on the first line (e.g. #!/bin/bash) and then [b]:
# Use ./ (or any valid dir spec):
./script.sh
Note: chmod +x does nothing to a script's executability on Git Bash. It won't hurt to run it, but it won't accomplish anything either.
#!/usr/bin/env sh
this is how git bash knows a file is executable. chmod a+x does nothing in gitbash. (Note: any "she-bang" will work, e.g. #!/bin/bash, etc.)
If you wish to execute a script file from the git bash prompt on Windows, just precede the script file with sh
sh my_awesome_script.sh
if you are on Linux or ubuntu write ./file_name.sh
and you are on windows just write sh before file name like that sh file_name.sh
For Linux -> ./filename.sh
For Windows -> sh file_name.sh
If your running export command in your bash script the above-given solution may not export anything even if it will run the script. As an alternative for that, you can run your script using
. script.sh
Now if you try to echo your var it will be shown. Check my the result on my git bash
(coffeeapp) user (master *) capstone
$ . setup.sh
done
(coffeeapp) user (master *) capstone
$ echo $ALGORITHMS
[RS256]
(coffeeapp) user (master *) capstone
$
Check more detail in this question
I had a similar problem, but I was getting an error message
cannot execute binary file
I discovered that the filename contained non-ASCII characters. When those were fixed, the script ran fine with ./script.sh.
Once you're in the directory, just run it as ./myScript.sh
If by any chance you've changed the default open for .sh files to a text editor like I had, you can just "bash .\yourscript.sh", provided you have git bash installed and in path.
I was having two .sh scripts to start and stop the digital ocean servers that I wanted to run from the Windows 10. What I did is:
downloaded "Git for Windows" (from https://git-scm.com/download/win).
installed Git
to execute the .sh script just double-clicked the script file it started the execution of the script.
Now to run the script each time I just double-click the script
#!/bin/bash at the top of the file automatically makes the .sh file executable.
I agree the chmod does not do anything but the above line solves the problem.
you can either give the entire path in gitbash to execute it or add it in the PATH variable
export PATH=$PATH:/path/to/the/script
then you an run it from anywhere

AIX script hangs when using /dev/null > 2>&1

I am trying to run a script in AIX to execute another script on a remote server. In addition to the remote script i need to send the stdout to /dev/null. The same command works fine on another server but when I run on the current server it hangs, any advice?
su - test -c "rsh testserver /scripts/testme" 2>&1 >/dev/null1
In your comment you write that a menu is presented when the user logins.
Let's say this is done in the .profile file, using echoes and a read command.
When a menu is presented, the read command in the menu code will not be skipped by redirecting the output. The menu still waits for your input and the su command seems to hang.
Can you change your .profile or .bashrc so that it will skip presenting the menu when called using a su command? When this is called during startup, you can look at the returncode of tty. When you use the su command from the commandline, you should look for another solution.
When your root shell is ksh, you can try the following:
if [[ "$(ps -fp $$)" != *"-ksh -c "* ]]; then
echo "Now I should call the Menu"
fi

How to make shell script run by double-click?

I have a script which is executable from command line but I want to make it user friendly as I want to run this script by double clicking on it. How it is possible?
#! /bin/bash
cd
cd Desktop/D_usman/
java -jar imageSynch.jar
You may need to add ".command" to the end of the name of your shellscript on Mac OS X.
You need to add execute permissions for the user, group, or rest of the world, depending on who should be allowed to execute it. Look into chmod for more information.
Example: chmod u+x myscript
Once you do this, you can also start the shell script like this ./myscript instead of sh myscript.sh
Note: You can also make your JAR start by adding execute permission, given Java is setup correctly on your machine.

Unable to run a postgresql script from bash

I am learning the shell language. I have creating a shell script whose function is to login into the DB and run a .sql file. Following are the contents of the script -
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
echo "Running SQL Dump - auto_qa_db_sync"
\\i auto_qa_db_sync.sql
After running the above script, I get the following error
./autoqa_script.sh: 39: ./autoqa_script.sh: /i: not found
Following one article, I tried reversing the slash but it didn't worked.
I don't understand why this is happening. Because when I try manually running the sql file, it works properly. Can anyone help?
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production and run script"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT -f auto_qa_db_sync.sql
The lines you put in a shell script are (moreless, let's say so for now) equivalent to what you would put right to the Bash prompt (the one ending with '$' or '#' if you're a root). When you execute a script (a list of commands), one command will be run after the previous terminates.
What you wanted to do is to run the client and issue a "\i ./autoqa_script.sh" comand in it.
What you did was to run the client, and after the client terminated, issue that command in Bash.
You should read about Bash pipelines - these are the way to run programs and input text inside them. Following your original idea to solving the problem, you'd write something like:
echo '\i auto_qa_db_sync.sql' | $DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
Hope that helps to understand.

Run commands in GNU Screen windows from .screenrc

Is there a way to send a sequence of commands to GNU Screen windows from my .screenrc? It seems like this should be easy to do:
.screenrc:
startup_message off
screen -t "RAILS SERVER"
<send command to last created window> <my alias to cd Rails project>
<send command to last created window> rails s
screen -t "RAILS CONSOLE"
<send command to last created window> <my alias to cd to Rails project>
rails c
I've gone over the Screen man-page several times, but can't find anything that will <send command to last created window>.
Thanks,
Max
Keith's answer gets the job done but it ties the window to that process so that as soon as the application is done executing, the window closes.
Here's what I wound up doing that worked perfectly:
screen -t "RAILS SERVER"
stuff "cd $my_rails_directory; rails server^M"
screen -t "RAILS CONSOLE"
stuff "cd $my_rails_directory; rails console^M"
The important part to note here is the ^M character. This isn't actually a ^ followed by an M. This is a raw newline character. In almost any CLI program (vi, emacs, shell), you can press CTRL-V and then press ENTER to generate this character.
How does this work? The stuff command types the given string directly into the console. The newline literal at the end actually sends the command off the way you normally would if you typed it yourself. Hope that helps! I've found this approach to be far more stable and reliable than others.
It's not a separate command; you just specify the command to run on the line that creates the window.
For example (untested):
screen -t "RAILS SERVER" sh -c "cd ... ; rails s"