Note that Slack now notifies you when a process for a time-consuming program (such as learning a deep model) is complete.
As a feature,
to hold.
Tools used
Shell script
Environment
I've touched shell scripts for almost the first time, so please point out any redundant coding.
Go to Incoming Webhook and link to the channel of your workspace.
I hope you can set it to your liking. You will need the Webhook URL
here for further work.
I created the following directory structure. I think it can be anywhere if it is easy to understand.
~/
└ Documents/
└ slack/
└ (I'll create it here)
First, create a shell script in ~/Documents/slack /
with the following command.
$ touch watch #I think the name can be anything.
Then open watch
in your editor. I wrote the following shell script there.
$ vi watch
watch
#!/bin/bash
set -eu
URL="" ###Write the Webhook URL here!###
USERNAME="Aoi Kiriya"
PID=$1
if [ ! $PID ]; then
echo "ERROR: PID required." >&2
exit 1
fi
#Process at the start of execution
START_MSG="`ps ho args $PID`"
ret=$?
if [ $ret -ne 0 ]; then
echo "ERROR: Process not found." >&2
exit 1
else
ary=(`echo $START_MSG`)
for i in `seq 1 ${#ary[@]}`
do
set +u
if [ "$i" -eq 1 ]
then
PRETXT="${ary[$i-1]##*/}I ran"
else
TEXT="$TEXT ${ary[$i-1]}"
fi
done
fi
set -u
TITLE="Execution command"
TEXT="\`\$ ${ary[0]##*/}$TEXT\`"
BEGIN_DATA="payload={\"username\": \"$USERNAME\", \"text\": \"Start tracking!(PID: \`$PID\` )\", \"attachments\": [{\"fallback\": \"Confirm execution command\",\"color\": \"#003399\",\"pretext\": \"$PRETXT\" ,\"title\": \"$TITLE\",\"text\": \"$TEXT\"}]}"
curl -s -X POST --data-urlencode "$BEGIN_DATA" ${URL} >/dev/null
#At the end of the process
END_MSG="${ary[0]##*/}Is finished\n Tracking finished!(PID: \`$PID\` )"
: "start watch ${PID}"
{
while true
do
if ! ps -p ${PID} >/dev/null ;then
curl -s -X POST --data-urlencode "payload={\"username\": \"$USERNAME\", \"text\": \"${END_MSG}\"}" ${URL} >/dev/null
exit
fi
sleep 1m
done
} &
I referred to this code. thank you very much.
Reference: https://gist.github.com/ohsuga/41a459792748e8c2afdb81d0b261c3de
Once you've done this, let's test it once.
First, create a source code that counts for 30 seconds in Python. Print os.getpid ()
so that the process ID is also output at the same time.
$ vi test.py
test.py
import os
import sys
import time
def print_args(pid, args):
print(os.getpid(), args)
if __name__ == "__main__":
args = sys.argv
print_args(os.getpid(), args)
for i in range(0, 30, 1):
time.sleep(1)
print(i, "sec")
And the test command is: Let's add various things as a test of command line arguments.
Here, the process ID is different for each process, so please respond flexibly. In the following cases, it is 17027
.
$ python test.py 1 2 3 hello --args-conf
17027 ['test.py', '1', '2', '3', 'hello', '--args-conf']
0 sec
1 sec
2 sec
3 sec
...
Execute the following command within 30 seconds.
$ bash watch 17027
Then, the following posts will be posted on the channel linked with Slack's Incoming Webhook. And when 30 seconds have passed. .. ..
26 sec
27 sec
28 sec
29 sec
$
I think there is a time lag, but I think the following posts will be posted.
Now that you've created the command, let's put it in your PATH so that you can access it from anywhere on your PC.
First, write the PATH of the shell script in ~/.bashrc
with the following command.
$ echo export "PATH=$HOME/Documents/slack:$PATH" >> ~/.bash_profile
$ source ~/.bash_profile
Let's also set an alias so that we can run it without hitting bash
. I made it run with the command aoi
.
$ echo alias aoi='bash watch' >> ~/.bash_aliases
$ source ~/.bash_aliases
Now try typing aoi
on the command line, and if you get the following error, you're in your PATH.
$ aoi
/home/user/Documents/slack/watch: line 11: $1: unbound variable
Let's try the PyTorch tutorial MNIST Classification Task. Please note that this requires torch
and torchvision
.
When you bring the code, first run the main code with the following command.
$ python main.py --no-cuda
Train Epoch: 1 [0/60000 (0%)] Loss: 2.305401
Train Epoch: 1 [640/60000 (1%)] Loss: 1.359780
Train Epoch: 1 [1280/60000 (2%)] Loss: 0.830692
Train Epoch: 1 [1920/60000 (3%)] Loss: 0.620273
Train Epoch: 1 [2560/60000 (4%)] Loss: 0.354148
Train Epoch: 1 [3200/60000 (5%)] Loss: 0.461558
Train Epoch: 1 [3840/60000 (6%)] Loss: 0.278612
...
Then check the process ID to run aoi
. You can check it with the following command.
$ ps x | grep "python main.py"
11313 pts/0 Rl+ 0:10 /home/user/.pyenv/versions/3.6.7/bin/python main.py --no-cuda
11387 pts/5 S+ 0:00 grep --color=auto python main.py
Probably the process ID is 11313
. Now let's run aoi
!
$ aoi 11313
Then. .. .. It came to notify the start and end of learning like this.
I think it is still in the development stage, so please be careful when introducing it m (_ _) m
As a future issue
I want to do it.
If you don't mind, we look forward to your contribution to the GitHub repository!
Recommended Posts