bundle exec command fails but gives exitcode 0 - ruby-on-rails

I tried running bundle exec create_namespace command on Ubuntu. It gives error but the exitcode = 0. Is there an alternative way to capture the failure of the bundle exec create_namespace command?

bundle exec executes a command in the context of the bundle (from bundle exec --help ).
If you pass a executable name that is not found, e.g. bundle exec thisisnotwhatyouarelookingfor, it will exit with a status code != 0.
"Your" command (create_namespace) must be a self-provided script, most likely something you are working on, or something located in the repository you play around with. It is then the responsability of that script to exit with the "correct" exit code.
You'd need to provide more information about your environment and that executable for people to dive into the issue. To find the executable yourself, a find . -name "create_namespace*" should point you in the right direction (most likely in ./binor./exe` if it is a gem and follows conventions).
TL;DR Most likely bundle exec does not fail (it starts the create_namespace "command" in the correct environment), but the command itself fails without setting the exit code properly.

Related

bundle command not found on bash script

I'm wrote a script to automatically run when reboot on crontab
this is my configuration in crontab -e
#reboot /home/deploy/startup_script >> /home/deploy/startup_script.log 2>$1
This start the script and create logs in /home/deploy
Then this is the startup_script
#!/bin/bash
echo "Changing directory"
cd /home/deploy/source/myapp
echo $PWD
echo "Pulling Dev Branch..."
git pull origin dev_branch
echo "Running Bundle Install"
sudo gem install bundler
bundle install
echo "Deploying to Staging..."
bundle exec cap staging deploy
when I run this script manually using ./startup_script it runs properly but when I run it automatically in crontab it shoes bundle command not found even I install the bundler already.
Here's the logs from startup_script.log
Changing directory
/home/deploy/source/myapp
Pulling Dev Branch...
From ssh://1.xx.xx.xx.io:20194/xx/myapp
* branch dev_branch -> FETCH_HEAD
Already up-to-date.
Running Bundle Install
Successfully installed bundler-2.0.2
Parsing documentation for bundler-2.0.2
Done installing documentation for bundler after 5 seconds
1 gem installed
/home/deploy/startup_script: line 12: bundle: command not found
Deploying to Staging...
/home/deploy/startup_script: line 15: bundle: command not found
The cron often clears the whole environment, including this $PATH variable. Therefore, the script may behave differently in your cron compared to the behavior in the shell. To avoid having to type the absolute path to a command, shells introduced the $PATH environment variable, each directory is separated by a : and searches are done from left to right.
Option I: You can use absolute path:
Run which bundle as sudoer to get the full path for the bundle command. If the output is /usr/bin/bundle, your bundle command in the script would look like:
/usr/bin/bundle install
Option II: Set the PATH variable:
Run echo "$PATH" as user who runs this script to get the $PATH variable and make sure this variable is available in your cron script too. For example, if the output was /usr/local/bin:/usr/bin:/bin, you would put the below line in the top of your shell script:
export PATH="/usr/local/bin:/usr/bin:/bin"
The environment that your crontab uses is going to be different than your regular login shell.
Now, I might be wrong about this, but I think when the crontab executes, it's not a login shell, so it doesn't have anything you've added to your path in your .bashrc or .bash_profile.
The best practice here would be to use the full path of the executable for bundle.
Redirecting stderr to stdout, there should be 2>&1
Is the path where the gem packages are installed is added to the $PATH variable? Try to provide the full path to this script
I suggest you make an entry to see what environment variables you have for crontab:
* * * * * printenv > ~/printenv.log

Launch application in gdb with non-path name

I am trying to launch a legacy application in GDB, and it requires that it's argv[0] value not contain anything other than alphanumeric characters.
Whenever I launch the program in GDB it seems that it expands the name to be the full path before running the program, so I get an error like (because it can't deal with the slashes):
"Cannot find /home/user/myapp ..."
Is it possible to run a program in GDB with a relative path, so that it will just see "myapp"?
Gdb normally runs target commands using the shell command line
exec program_pathname program_arguments
But it has a set exec-wrapper command that will change this to
exec exec_wrapper program_pathname program_arguments
The exec_wrapper is often another command, but it can be an option that the exec command accepts.
Many shells (bash, zsh, ksh93) support a -a option to the exec command to set argv[0].
So, if your shell supports exec -a, you can do the following to invoke /home/user/myapp with argv[0]==myapp:
(gdb) set exec-wrapper -a myapp

bundler: command not found: rails with docker-compose on a sample project

I'd like to develop a rails app using docker so I don't have to install a database locally. Therefore I created the following basic example (new rails app):
https://gist.github.com/solars/62eeae2f86ab6ec3fa35
As you can see there is a problem with the bundler environment, can anyone tell me how to fix this?
Usually the Gemfile is copied over in the Dockerfile - but I think this is not necessary if mounting the app in a volume, is this right? (Most of the examples are using ADD or COPY for either the Gemfile or the app, but as I'm developing, I'd like to avoid that so I can always change things.
I also thought that something like docker-compose run app echo $PATH should return the $PATH in my container, but it seems to be the same as my local path? Same with echo $RAILS_ENV which returns nothing, although it's set in the compose file..
Solution: The container of course does not persist the bundled gems, that was the problem. So the solution is to either bundle in the Dockerfile, or have the gems in the app directory which is shared as a volume.
docker-compose run app echo $PATH will always return your local path, as the environment variable is interpreted by the local shell.
This would work better:
docker-compose run app sh -c 'echo $PATH'
Regarding rails, check "Rails 4 bundler: command not found: rails"
A bundle exec rake rails:update:bin might help (for instance, as a RUN directive in your Dockerfile).

Invoke rake jobs:work automatically after running rails s in console

Can I invoke "rake jobs:work" automatically after running "rails s" in console?
Currently, after running rails s in cmd I will also run rake jobs:work in the other console, what i want to happen is After running "rails s" the jobs:work will automatically start.
The right way to go about this would be to use a process manager, like Invoker or Foreman. There is ample documentation on the links, but it boils down to the following steps:
Install the software
Create a configuration file where you declare what processes do you intend to run. Both support Procfile style declaration.
Use the command line client to start the process manager.
Based on my personal experience, I highly recommend Invoker, it goes beyond just a process manager, and packs in a few more handy features, like support for .dev local domain.
One you can do is simply:
rails server & rake jobs:work
It'll run rails server as background job, which you can get back to foreground with fg. It can be annoying that you'll get output from both processes mixed.
I'm not sure what are your needs and what you expect but maybe it would be good for you to use screen (or tmux) to run them in parallel and be able to switch between.
You can do your own .screenrc script which will run the server and any other commands when automatically for you.
There is a little problem that if you run the server from it and you close it (ctrl+c) than you'll loose it's screen window. Fortunately there is a solution for that as well (worked-out on the SO as well - you can read more about it here)
So, I use some helper script for that .run_screen (don't forget to chmod +x it):
#!/bin/bash
/bin/bash -i <<<"$*; exec </dev/tty"
Than I have .screenrc_rails file:
#shell -${SHELL}
caption always "%n(%t) %= %{b}#%H[%l] : %{r}%c:%s"
termcapinfo xterm ti#:te#
termcap xterm 'AF=\E[3%dm:AB=\E[4%dm'
terminfo xterm 'AF=\E[3%p1%dm:AB=\E[4%p1%dm'
startup_message off
screen -t server 2 ${HOME}/.run_screen rails s
screen -t spork 3 ${HOME}/.run_screen bundle exec spork
screen -t dev_log 4 ${HOME}/.run_screen tail -f ./log/development.log
screen -t test_log 5 ${HOME}/.run_screen tail -f ./log/test.log
screen -t bash 0
screen -t bash 1
And an alias ( screenr(ails) ) defined at .bash_profile:
alias screenr='screen -c ~/.screenrc_rails'
If you don't know screen than start from ctrl+a, ". ctrl+a, ? will give you some help.
I hope you'll enjoy it.

bundle exec not working with crontab

I'm trying to execute the following shell script using crontab:
#!/bin/sh
cd /mnt/voylla-production/current
bundle exec rake maintenance:last_2_days_orders
bundle exec rake maintenance:send_last_2_days_payment_dropouts
The crontab entry is
0 16 * * * /mnt/voylla-production/releases/20131031003111/voylla_scripts/cj_4pm.sh
I'm getting the following error message in the mail:
/mnt/voylla-staging/current/voylla_scripts/cj_4pm.sh: line 3: bundle: command not found
/mnt/voylla-staging/current/voylla_scripts/cj_4pm.sh: line 4: bundle: command not found
I dont get the error when I run the commands manually. Not sure what's going on here. Could someone please point out.
Thanks
A nice trick to get all environment properly set up in crontab is to use /bin/bash -l :
0 16 * * * /bin/bash -l -c '/mnt/voylla-production/releases/20131031003111/voylla_scripts/cj_4pm.sh'
The -l option will invoke a full login shell, thus reading your bashrc file and any path / rvm setting it performs.
If you want to simplify your crontab management and use this trick - as well as others - without having to think about them, you can use the Whenever gem. It also play very nice with capistrano, if you use it, regenerating crontab on deploy.
The user used by cron does not have the correct environment.
You can tell cron which user to use. For a bash script, you can so something like:
#!/bin/bash --login
source /home/user/.bashrc
rvm use 2.0.0#gemset #if you use rvm
cd /path/to/project && bundle exec xyz
We need set right path to our bundle:
#!/bin/sh
cd /mnt/voylla-production/current
/home/youruser/.rbenv/shims/bundle exec rake maintenance:last_2_days_orders

Resources