Grunt task that restarts my app via command line - bash

I'm looking for a Grunt task that would restart my Sails app. Currently, the only way I know how to restart a Sails app is going to the terminal where the app is running and enter 'Ctrl + C'. Then 'Sails lift' to start it up again. Is there a way that Grunt can run these two commands for me?
I got close using this: https://github.com/sindresorhus/grunt-shell, but couldn't quite take it home. Any advice?
EDIT:
The following successfully kill the Sails app via the command line, but the Grunt task (below) still causes an error when I try to start the Sails app:
pkill -2 node /usr/local/bin/sails and pkill -SIGINT node /usr/local/bin/sails
The code in my Grunt task is:
module.exports = function(grunt) {
grunt.config.set('shell', {
multiple: {
command: [
'pkill -SIGINT node /usr/local/bin/sails',
'sails lift'
].join('&&')
}
});
grunt.loadNpmTasks('grunt-shell');
};
(I'm using this Grunt task: https://github.com/sindresorhus/grunt-shell)
And when I try to start the app ("Sails lift") I get a "Command failed" error. I think it has to do with the fact that I'm trying to kill the app with the app itself. Or something.

pkill can be used to kill a process based on its name instead of PID
pkill -SIGINT {name of process} && Sails lift
This should do what you need. You can find the name of the process pretty easily by doing a top or a ps aux | grep "sail"
Im sure the process is just sails, so it would likely be:
pkill -SIGINT sails && Sails lift

Ctrl + C sends SIGINT (see signal(7)), so if you can use shell commands, use the kill command like this:
kill -SIGINT {pid-of-your-Sails-app} && Sails lift

I've been using nodemon for this task and it works great:
https://github.com/remy/nodemon

Related

How to run Electron app independent of terminal?

I am looking for a way to run an Electron app (npm startcommand) independently of the terminal itself. Meaning that I expect the Electron app to keep running even if the terminal closes..
I am not sure whether it is possible.
I have tried cd electron-directory-path && nohup npm start &, but this though allows me to use the terminal instance for other commands and prevents any electron messages from popping up in the terminal. But, closing the terminal still kills the Electron app.
Even cd electron-directory-path && npm start & does the same thing, but I haven't yet been able to find a way to run the Electron app completely independent of the terminal instance...
You start an Electron app through nohup npm start &, but when closing the terminal window, the Electron app also terminates (against expectation).
I can reproduce the behavior, but not all the times. In roughly 30% of my experiments, the Electron app was not terminated. I was not able to find the reason for this varying behavior yet.
Workaround
The following workaround closes the terminal without terminating the Electron app. In my tests, it has worked every time:
Start the Electron app as before: nohup npm start &
Close the running terminal by issuing nohup kill $$ &
The $$ gives the current process id.
Note that kill $$ doesn't work.
If you don't necessarily need to run from a terminal, you can also create a desktop file to start the app.
Let pathname be the path to the node app location.
Just use the command:
cd pathname && npm start && ^Z &
cd to change directory to where we need to execute the terminal command.
&& to mean there are other commands to be executed after this one.
npm start to start npm app
^Z to suspend the process running in the terminal, and hence disconnect the terminal part of node from the original app.
& to mean that we don't want the terminal to wait for the command to execute.
Now we can close the terminal, and the electron app should keep running...!
Credits:
https://tecadmin.net/close-terminal-without-killing-running-processes-on-linux/

Automatically terminate all nodes after calling roslaunch

I am trying to run several roslaunch files, one after the other, from a bash script. However, when the nodes complete execution, they hang with the message:
[grem_node-1] process has finished cleanly
log file: /home/user/.ros/log/956b5e54-75f5-11e9-94f8-a08cfdc04927/grem_node-1*.log
Then I need to Ctrl-C to get killing on exit for all of the nodes launched from the launch file. Is there some way of causing nodes to automatically kill themselves on exit? Because at the moment I need to Ctrl-C every time a node terminates.
My bash script looks like this, by the way:
python /home/user/git/segmentation_plots/scripts/generate_grem_launch.py /home/user/Data2/Coco 0 /home/user/git/Async_CNN/config.txt
source ~/setupgremsim.sh
roslaunch grem_ros grem.launch config:=/home/user/git/Async_CNN/config.txt
source /home/user/catkin_ws/devel/setup.bash
roslaunch rpg_async_cnn_generator conf_coco.launch
The script setupgremsim.sh sources another catkin workspace.
Many thanks!
Thanks all for your advice. What I ended up doing was this; I launched my ROS Nodes from separate python scripts, which I then called from the bash script. In python you are able to terminate child processes with shutdown. So to provide an example for anyone else with this issue:
bash script:
#!/bin/bash
for i in {0..100}
do
echo "========================================================\n"
echo "This is the $i th run\n"
echo "========================================================\n"
source /home/timo/catkin_ws/devel/setup.bash
python planar_launch_generator.py
done
and then inside planar_launch_generator.py:
import roslaunch
import rospy
process_generate_running = True
class ProcessListener(roslaunch.pmon.ProcessListener):
global process_generate_running
def process_died(self, name, exit_code):
global process_generate_running
process_generate_running = False
rospy.logwarn("%s died with code %s", name, exit_code)
def init_launch(launchfile, process_listener):
uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)
launch = roslaunch.parent.ROSLaunchParent(
uuid,
[launchfile],
process_listeners=[process_listener],
)
return launch
rospy.init_node("async_cnn_generator")
launch_file = "/home/user/catkin_ws/src/async_cnn_generator/launch/conf_coco.launch"
launch = init_launch(launch_file, ProcessListener())
launch.start()
while process_generate_running:
rospy.sleep(0.05)
launch.shutdown()
Using this method you could source any number of different catkin workspaces and launch any number of launchfiles.
Try to do this
(1) For each launch you put in a separate shell script. So you have N script
In each script, call the launch file in xterm. xterm -e "roslaunch yourfacnylauncher"
(2) Prepare a master script which calling all N child script in the sequence you want it to be and delay you want it to have.
Once it is done, xterm should kill itself.
Edit. You can manually kill one if you know its gonna hang. Eg below
#!/bin/sh
source /opt/ros/kinetic/setup.bash
source ~/catkin_ws/devel/setup.bash
start ROScore using systemd or rc.local using lxtermal or other terminals to avoid accident kill. Then run the part which you think gonna hang or create a problem. Echo->action if necessary
xterm -geometry 80x36+0+0 -e "echo 'uav' | sudo -S dnsmasq -C /dev/null -kd -F 10.5.5.50,10.5.5.100 -i enp59s0 --bind-dynamic" & sleep 15
Stupid OUSTER LIDAR cant auto config like Veloydne and will hang here. other code cant run
killall xterm & sleep 1
Lets just kill it and continuous run other launches
xterm -e "roslaunch '/home/uav/catkin_ws/src/ouster_driver_1.12.0/ouster_ros/os1.launch' os1_hostname:=os1-991907000715.local os1_udp_dest:=10.5.5.1"

kill background process spawn in codeship

To kill background process inside Codeship we need to use following command:
#!/bin/bash
nohup bash -c "YOUR_COMMAND 2>&1 &"
The bash -c is needed in codeship but because this killing YOUR_COMMAND fails when storing it inside variable using PID_COMMAND=$!. It could be I am missing/doing something (wrong). But I have googled a lot and could not find correct answer so I hope you guys can help me.
FWIW:
I started a flask app with: flask run &
To kill the background process, I use pgrep & kill to find and terminate the flask instance:
kill $(pgrep -f flask)
Replace 'flask' with whatever name your command/program was

How do you run a shell command in the background and suppress all output?

I'm trying to write a Shell script (for use in Mac OSX Termninal) that will run a command to start a development server (gulp serve). I have it working except the server is continuously running so it doesn't allow me to enter subsequent commands in the same window without stopping the server (Control+C). My question is, is there a way I can run the process in the background and/or suppress any/all output? My goal is to also write a 'stop server' command that will kill the process (which I'm also unsure how to do). I've tried all combinations of using ampersands and &>/dev/null and nothing quite works. Here's what I have so far:
if [ "$1" = "server" ]
then
if [ "$2" = "on" ]
then
cd / & gulp serve --gulpfile /server/example/gulpfile.js # the output is still shown
printf "\033[0;32mserver is online.\033[0m\n"
else
killall tail &>/dev/null 2>&1 # this doesn't kill the process
printf "\033[0;32mportals is offline.\033[0m\n"
fi
fi
You're doing the output redirection on killall, not gulp, so gulp will continue to merrily split out text to your terminal. Try instead:
cd / && gulp server --gulpfile /server/example/gulpfile.js >/dev/null 2>&1 &
Secondly, your kill command doesn't kill your process because you're not telling it to; you're asking it to kill all tail processes. You want instead:
killall gulp
These modifications should be the most direct path to your goal. However, there are a few additional things that may be useful to know.
Process management has a long history in the *nix world, and so we've been inventing tools to make this easier for a long time. You can go through re-inventing them yourself (the next step would be to store the PID of your gulp process so that you can ensure you only kill it and not anything else with "gulp" in the name), or you can go all the way and write a system monitoring file. For Linux, this would be SysV, Upstart, or systemd; I'm not sure what the OS X equivalent is.
However, since you're just doing this for development purposes, not a production website, you probably don't actually need that; your actual goal is to be able to execute ad-hoc shell commands while gulp is running. You can use terminal tabs to do this, or more elegantly use the splitting capabilities of iTerm, screen, or tmux. Tmux in particular is a useful tool for when you find yourself working a lot in a terminal, and would be a useful thing to become familiar with.
First, to run the process in the background
cd / && gulp serve --gulpfile /server/example/gulpfile.js > /tmp/gulp.log &
after cd you need && (and) and & to run in the background at the end.
To kill all gulp processes
killall gulp

Halt grunt server from terminal

I've installed and am exploring Yeoman but one thing that I can't seem to find an answer to is, how do I halt the grunt server from running?
Is this something that can be done from the terminal? It just sits and waits.
You can stop it with ctrl + c in terminal
If it's a task that is running in background you can find his process
id (pid) with ps aux | grep grunt and then kill it with kill {pid}

Resources