closing windows shell after rake execution - ruby

I need to execute a rake task in a windows machine and unfortunately to do so I have to open a shell. How can I close it once the task has been completed?
system('start rake db:MyTask')
Thanks,.

maybe something like
system('start rubyw -S rake db:MyTask')
are you running this from the "run" box? Why is a window popping up?

Related

why rake tasks are not executing using and operator?

I have a rake task :
task :kill_process do
system %q(ps -ef | awk '{if($8~"java" || $8~"glassfish" || $8~"ruby" || $8~"god" || $8~"couch"){printf("Killing : %s \n",$2);{system("kill -9 "$2)};}}')
end
This is basically killing processes. And this task is a part of another rake task :
desc "stop the entire system"
task :stop => [...., :kill_process]
There's another task:
desc "start the entire system"
task :start => [....]
When I am doing rake stop && rake start
stop task is executed successfully. but rake start is not executing.
If i execute both tasks separately, then it works fine. but not in rake stop && rake start
What will be better to use here exec function or system or any other, please suggest me.
My only requirement is to kill these mentioned processes at the end of rake stop. But also it should not effect other things like rake stop && rake start should work fine.
As mentioned in the comments, the exit code is 137 which evaluates to false and therefore the other part of the && does not get executed. The reason for this is probably kill -9.
There are a few options now.
Return 0 from your rake task, something like exit(0)
Don't use kill -9
Create restart command which does execute stop and start but without logically depending on each other (&&).
Exit code 137 indicates that a process has received a SIGKILL signal and was thus killed from the outside.
This happens since a Rake task is also executed by Ruby. As such, your stop task is sending a SIGKILL to its own process too (along with all other Ruby processes on the system). Now, since you have specified that you only want to execute the rake start process if the previous process was successful (i.e. had a exit code of 0), your shell doesn't start the rake task.
To quickly fix this, you can instead run rake stop; rake start, i.e run the two processes regardless of their individual exit codes (by default).
However, a better idea would probably to make your stop task more explicit and only kill the specific processes you need rather than everything in sight which looks slightly like a related process. This will likely result in a more stable system overall too when you don't kill potentially unrelated processes all the time.

resque rake task giving error

I have been using resque for background processing, No my problem with code is :
- when I start rake task as "rake resque:work QUEUE=''" as per ryan bates episode no. 271. in remote server the code inside worker class for file maipulation works properly without any filepath issues and I/O errors.
- when i start rake task as "rake resque:work QUEUE='' BACKGROUND=yes" now, the code inside worker class gives "failed:Errno::EIO: Input/output error # io_write - >" error.
Now my question is I want to start the resque queue above rake command only one time and why second point giving error is this issue with filepaths if so then why it runs smoothly as mention in point first.
You can use god to manage your background process. Or nohup can be your solution too as below:
$ nohup bundle exec rake resque:work QUEUE=queue_name PIDFILE=tmp/pids/resque_worker_QUEUE.pid & >> log/resque_worker_QUEUE.log 2>&1
and even this command worked for me:
PIDFILE=./resque.pid BACKGROUND=yes QUEUE="*" rake resque:work >> worker1.log &
Hope that will help you too.

How can I create a monit process for a Ruby program?

I have these rake tasks that will occasionally fail. I want to use monit to monitor them and to restart them if necessary.
I have read the other ruby/monit threads on StackOverflow. My case is different in that these programs require my Rails environment in order to work. That's why I have them as rake tasks now.
Here is one of the tasks I need to monitor, in it's entirety:
task(process_updates: :environment) do
`echo "#{Process.pid}" > #{Rails.root}/log/process_alerts.pid`
`echo "#{Process.ppid}" > #{Rails.root}/log/process_alerts.ppid`
SynchronizationService::process_alerts
end
My question is, do I leave this as a rake task, since SynchronizationService::process_alerts requires the Rails environment to work? Or is there some other wrapper I should invoke and then just run some *.rb file?
Monit can check for running pid, since you're creating pid when you run task you can create a monit config which should look something like this:
check process alerts with pidfile RAILSROOT/log/process_alerts.pid
start program = "cd PATH_TO_APP; rake YOURTASK" with timeout 120 seconds
alert your#mail.com on { nonexist, timeout }
Of course RAILSROOT, PATH_TO_APP, YOURTASK should correspond to your paths/rake task.
Monit then will check for running process in system using the pidfile value and will start the process using start program command if it can't find running process.

Rails.vim - How to test all features and/or specs?

I am using rails.vim plugin which is pretty awesome. However, I fail to see how could I test all the specs in one command. Right now I need to open a particular spec and do :Rake and that just tests the current opened spec. However, how could I test all the specs? Which command?
Thanks
Have you tried pairing a rake task with a ViM leader mapping?
In your Rakefile, you could set up something like this:
desc 'Continuous integration task'
task :ci do
['rspec',
'cucumber -f progress',
'rake konacha:run'].each do |cmd|
system("bundle exec #{cmd}")
raise "#{cmd} failed!" unless $?.exitstatus == 0
end
end
Then you can setup a leader command in ViM to execute your ci rake task:
nnoremap <leader>T :w\|:!bundle exec rake ci<CR>
Then when you execute <leader> T in normal mode, ViM will shell out and run bundle exec rake ci.
I use tmux, so I prefer the following leader mapping which runs the rake task in a bottom pane:
nnoremap <leader>T :w\|:silent !tmux send-keys -t bottom C-u 'bundle exec rake ci' C-m <CR>\|:redraw!<CR>

Strange behavior of "Run in terminal", it kills processes

I wrote a shell script that runs a service. I open the terminal, I run the script that runs the service and, after the script ends, I close the terminal but the service keeps running, and this is what I want.
Anyway, if I run the script through the Gnome command "Run in terminal", when the terminal closes, also the service is killed.
That's very strange, I can't understand why and I'm not able to solve this problem.
Any ideas?
Try executing
nohup ./shell_script &
nohup command makes the process continue executing even after the terminal has closed, ignoring the SIGHUP signal.
Note the script will execute in the background, and the output will be appended to a file.

Resources