Skip to content

Commit

Permalink
bug when checking processes missions in narrow terminals
Browse files Browse the repository at this point in the history
GNU ps uses COLUMNS to truncate its output,
even when output is not on a tty!
As a result, the command name might get truncated!

That's a little strange...

I set COLUMNS to 512 to avoid that, but that's not very satisfying
  • Loading branch information
phyver committed Jun 7, 2021
1 parent 763ed2f commit c849e33
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion missions/processes/01_ps_kill/auto.sh
@@ -1,2 +1,2 @@
ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
gsh check
8 changes: 7 additions & 1 deletion missions/processes/01_ps_kill/check.sh
@@ -1,8 +1,14 @@
#!/bin/bash

# we need to use ps -c to only get the command name and not the full command in
# macOS
# the command name is pretty long as it the processes are not in the path and
# are given as absolute path
# GNU ps truncates the output according to COLUMNS, even when output is not on
# a tty, hence we set COLUMNS to 512 which should be long enough.
_mission_check() {
local pid=$(cat "$GSH_VAR/spell.pid")
if ps -cp $pid | grep "$(gettext "spell")" > /dev/null
if COLUMNS=512 ps -cp $pid | grep "$(gettext "spell")" > /dev/null
then
echo "$(gettext "The spell is still running!")"
return 1
Expand Down
2 changes: 1 addition & 1 deletion missions/processes/01_ps_kill/test.sh
@@ -1,4 +1,4 @@
ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
gsh assert check true

gsh assert check false
4 changes: 2 additions & 2 deletions missions/processes/02_ps_kill_signal/auto.sh
@@ -1,3 +1,3 @@
ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null
ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
gsh check
8 changes: 7 additions & 1 deletion missions/processes/02_ps_kill_signal/check.sh
@@ -1,7 +1,13 @@
#!/bin/bash

# we need to use ps -c to only get the command name and not the full command in
# macOS
# the command name is pretty long as it the processes are not in the path and
# are given as absolute path
# GNU ps truncates the output according to COLUMNS, even when output is not on
# a tty, hence we set COLUMNS to 512 which should be long enough.
_mission_check() {
local nb_spells=$(ps -ce | grep "$(gettext "spell")" | wc -l | tr -d ' ')
local nb_spells=$(COLUMNS=512 ps -ce | grep "$(gettext "spell")" | wc -l | tr -d ' ')

if [ "$nb_spells" -gt 0 ]
then
Expand Down
12 changes: 6 additions & 6 deletions missions/processes/02_ps_kill_signal/test.sh
@@ -1,11 +1,11 @@
ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null
ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
gsh assert check true

ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill -9 2> /dev/null
gsh assert check false

ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null
ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null
ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | head -n2 | xargs kill -9 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | xargs kill 2> /dev/null
COLUMNS=512 ps -ce | awk -v spell="$(gettext "spell")" '$0 ~ spell {print $1}' | head -n2 | xargs kill -9 2> /dev/null
gsh assert check false
6 changes: 3 additions & 3 deletions missions/processes/03_pstree_kill/auto.sh
@@ -1,10 +1,10 @@
kill_imp_spell() {
local p=$(ps -ce | grep "$(gettext "mischievous_imp")" | awk '{print $1}')
local p=$(COLUMNS=512 ps -ce | grep "$(gettext "mischievous_imp")" | awk '{print $1}')
# argh, ps -eo ... doesn't find any process in macos-latest
# and ps -ceo ... doesn't find any process in ubuntu-latest
# let's try both!
ps -ceo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null
ps -eo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null
COLUMNS=512 ps -ceo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null
COLUMNS=512 ps -eo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null
}
cellar=$(eval_gettext '$GSH_HOME/Castle/Cellar')

Expand Down
14 changes: 10 additions & 4 deletions missions/processes/03_pstree_kill/check.sh
@@ -1,29 +1,35 @@
#!/bin/bash

# we need to use ps -c to only get the command name and not the full command in
# macOS
# the command name is pretty long as it the processes are not in the path and
# are given as absolute path
# GNU ps truncates the output according to COLUMNS, even when output is not on
# a tty, hence we set COLUMNS to 512 which should be long enough.
_mission_check() {

local pid
pid=$(cat "$GSH_VAR"/fairy.pid)
if ! ps -cp $pid | grep "$(gettext "nice_fairy")" > /dev/null
if ! COLUMNS=512 ps -cp $pid | grep "$(gettext "nice_fairy")" > /dev/null
then
echo "$(gettext "Did you kill the fairy?")"
return 1
fi
pid=$(cat "$GSH_VAR"/imp.pid)
if ! ps -cp $pid | grep "$(gettext "mischievous_imp")" > /dev/null
if ! COLUMNS=512 ps -cp $pid | grep "$(gettext "mischievous_imp")" > /dev/null
then
echo "$(gettext "Did you kill the imp?")"
return 1
fi

local nb=$(ps -cp $(cat "$GSH_VAR"/fairy_spell.pids) | grep "$(gettext "spell")" | wc -l)
local nb=$(COLUMNS=512 ps -cp $(cat "$GSH_VAR"/fairy_spell.pids) | grep "$(gettext "spell")" | wc -l)
if [ "$nb" -lt 3 ]
then
echo "$(gettext "Did you remove some of the fairy's spells?")"
return 1
fi

local nb=$(ps -cp $(cat "$GSH_VAR"/imp_spell.pids) | grep "$(gettext "spell")" | wc -l)
local nb=$(COLUMNS=512 ps -cp $(cat "$GSH_VAR"/imp_spell.pids) | grep "$(gettext "spell")" | wc -l)
if [ "$nb" -ne 0 ]
then
echo "$(gettext "Are you sure you removed all the imp's spells?")"
Expand Down
6 changes: 3 additions & 3 deletions missions/processes/03_pstree_kill/test.sh
@@ -1,10 +1,10 @@
kill_imp_spell() {
local p=$(ps -ce | grep "$(gettext "mischievous_imp")" | awk '{print $1}')
local p=$(COLUMNS=512 ps -ce | grep "$(gettext "mischievous_imp")" | awk '{print $1}')
# argh, ps -eo ... doesn't find any process in macos-latest
# and ps -ceo ... doesn't find any process in ubuntu-latest
# let's try both!
ps -ceo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null
ps -eo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null
COLUMNS=512 ps -ceo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null
COLUMNS=512 ps -eo pid,comm,ppid | grep "$p$" | grep "$(gettext "spell")" | awk '{print $1}' | xargs kill -9 2>/dev/null
}
cellar=$(eval_gettext '$GSH_HOME/Castle/Cellar')

Expand Down

0 comments on commit c849e33

Please sign in to comment.