mirror of https://github.com/01-edu/public.git
feat(job-regist): add new bash exercise
- are the current test cases enough?
This commit is contained in:
parent
7b30ef2edf
commit
81cd3cbd11
|
@ -0,0 +1,59 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# create a function to be called everytime the process exit
|
||||||
|
abort () {
|
||||||
|
rm exp.log job.log
|
||||||
|
}
|
||||||
|
|
||||||
|
trap 'abort' EXIT
|
||||||
|
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
IFS='
|
||||||
|
'
|
||||||
|
script_dirS=$(cd -P "$(dirname "$BASH_SOURCE")" &>/dev/null && pwd)
|
||||||
|
|
||||||
|
SUBMITTED='student/job-regist.sh'
|
||||||
|
EXPECTED='solutions/job-regist.sh'
|
||||||
|
|
||||||
|
wait_bg_jobs () {
|
||||||
|
while [ -n "$(jobs | grep -i running)" ]; do
|
||||||
|
echo -n "."
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
# test cases
|
||||||
|
one_process () {
|
||||||
|
sleep 2 &
|
||||||
|
source $script_dirS/$1
|
||||||
|
}
|
||||||
|
|
||||||
|
two_processes () {
|
||||||
|
sleep 3 &
|
||||||
|
sleep 4 &
|
||||||
|
source $script_dirS/$1
|
||||||
|
}
|
||||||
|
|
||||||
|
one_process_and_kill () {
|
||||||
|
echo do something
|
||||||
|
}
|
||||||
|
|
||||||
|
one_process_and_suspend () {
|
||||||
|
echo do something
|
||||||
|
}
|
||||||
|
# end of test cases
|
||||||
|
|
||||||
|
challenge () {
|
||||||
|
echo "testing $1 case"
|
||||||
|
$1 $SUBMITTED &
|
||||||
|
$1 $EXPECTED &
|
||||||
|
|
||||||
|
wait_bg_jobs
|
||||||
|
|
||||||
|
diff <(cat exp.log) <(cat job.log)
|
||||||
|
}
|
||||||
|
|
||||||
|
challenge one_process
|
||||||
|
challenge two_processes
|
|
@ -0,0 +1,11 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
IFS='
|
||||||
|
'
|
||||||
|
PID=$(jobs -l %1 | grep -Eo '[\+\-] [0-9]+' | grep -Eo '[0-9]+')
|
||||||
|
LOG_FILE="exp.log"
|
||||||
|
|
||||||
|
while kill -0 "$PID" 2> /dev/null; do
|
||||||
|
echo $(date +"%F %T") - $(jobs %1) >> "$LOG_FILE"
|
||||||
|
sleep 1
|
||||||
|
done
|
|
@ -0,0 +1,33 @@
|
||||||
|
## job-regist
|
||||||
|
|
||||||
|
### Instruction
|
||||||
|
|
||||||
|
Create a script, `job-regist.sh`, that will be able to monitor a specific background job.
|
||||||
|
|
||||||
|
The script needs to track the status of the first jobs spawned by the current terminal and periodically save the status into a `job.log` file. It must append the new update without modifying the previous content.
|
||||||
|
|
||||||
|
Each update needs to be appended to the file with the current format: `YYYY-MM-DD hh:mm:ss - <job status>`
|
||||||
|
|
||||||
|
The script should stop running as soon as the job it is tracking ends.
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
Here an example of how the script should behave:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ sleep 10 &
|
||||||
|
$ sleep 12 &
|
||||||
|
$ source job-regist.sh
|
||||||
|
$ cat job.log
|
||||||
|
2023-02-08 10:37:50 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:51 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:52 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:53 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:54 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:55 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:56 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:57 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:58 - [1]+ Running sleep 10 &
|
||||||
|
2023-02-08 10:37:59 - [1]+ Running sleep 10 &
|
||||||
|
$
|
||||||
|
```
|
Loading…
Reference in New Issue