-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashrun.sh
More file actions
executable file
·95 lines (65 loc) · 1.98 KB
/
bashrun.sh
File metadata and controls
executable file
·95 lines (65 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
#Bash script to execute each individual task.
echo "Hello world from BashRUN!"
#User name.
task_user=$1
#echo "1 is $task_user"
#Task name.
task_name=$2
#echo "2 is $task_name"
#Task id.
task_id=$3
#echo "3 is $task_id"
#User email address.
email=$4
#echo "4 is $email"
#Array of commands (comma separated).
num_of_commands=$5
#echo "5: $num_of_commands"
#List of commands
commands=$6
#echo "6: $commands"
#Remove first and last quotes.
commands=$(sed -e 's/^"//' -e 's/"$//' <<<"$commands")
#echo "7: $commands"
#Change the directory. move to the task directory
cd /var/www/html/worker/uploads/$task_id
#Unzip the compressed file
unzip `ls *.zip`
#Chage directory inside the newly generated folder
cd `ls -d -- */`
#Execute all the commands, one after the other.
for (( i=1; i<$num_of_commands; i++ ));do
command=$(echo $commands| cut -d',' -f $i)
echo "command is: $command"
$command
done
#Compress the complete task directory
zip -r $task_id.zip *
#Copy the results to the finished tasks directory
cp $task_id.zip /var/www/html/worker/finished
#Prepare email to send it to user
TO_ADDRESS=$email
FROM_ADDRESS="work@worker.de"
SUBJECT="Worker: $task_name Task finished"
BODY="Hello $task_user! Your task $task with id: $task_id is finished."
#Send email to user
echo "${BODY}" | mail -s "${SUBJECT}" "${TO_ADDRESS}" # -- -r "${FROM_ADDRESS}"
#Go back to the root directory.
cd /var/www/html/worker
#Search for the task in the file and chage status to FINISHED
filename="TasksList.csv"
while IFS='' read -r task || [[ -n "$task" ]]; do
#Get the id for each task in the TasksList file
file_task_id=$(echo $task| cut -d',' -f 3)
#echo $file_task_id
#If id is equal to current task_id, change status to FINISHED
if [[ $file_task_id == $task_id ]]; then
#replace the word RUNNING with FINISHED
sed -i '0,/\bRUNNING\b/{s/\bRUNNING\b/FINISHED/}' "$filename"
break
fi
done < $filename
#Done
echo "done!"
echo "---------------------------------------------------------"