forked from opentensor/bittensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_update.sh
executable file
·88 lines (71 loc) · 2.44 KB
/
auto_update.sh
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
#!/bin/bash
# Initialize variables
script=""
proc_name="auto_run_bittensor"
args=()
# Check if pm2 is installed
if ! command -v pm2 &> /dev/null
then
echo "pm2 could not be found. To install see: https://pm2.keymetrics.io/docs/usage/quick-start/"
exit 1
fi
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--script) script="$2"; shift ;;
--name) name="$2"; shift ;;
--*) args+=("$1=$2"); shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Check if script argument was provided
if [[ -z "$script" ]]; then
echo "The --script argument is required."
exit 1
fi
branch=$(git branch --show-current) # get current branch.
echo watching branch: $branch
echo pm2 process name: $proc_name
# Get the current file hash
current_hash=$(git log -n 1 --pretty=format:%H -- $script)
echo current_hash: $current_hash
# Check if script is already running with pm2
if pm2 status | grep -q $proc_name; then
echo "The script is already running with pm2. Stopping and restarting..."
pm2 delete $proc_name
fi
# Run the Python script with the arguments using pm2
echo "Running $script with the following arguments with pm2:"
echo "${args[@]}"
pm2 start "$script" --name $proc_name --interpreter python3 -- "${args[@]}"
while true; do
# Fetch the latest changes from the repository
git fetch origin $branch
# Get the latest file hash
latest_hash=$(git log -n 1 --pretty=format:%H -- origin $branch -- $script)
echo "current script hash:" "$current_hash"
echo "latest script hash:" "$latest_hash"
# If the file has been updated
if [ "$current_hash" != "$latest_hash" ]; then
echo "The file has been updated. Updating the local copy."
# Pull the latest changes
git pull
# Update the current file hash
current_hash=$latest_hash
# Check if script is already running with pm2
if pm2 status | grep -q $proc_name; then
echo "The script is already running with pm2. Stopping and restarting..."
pm2 delete $proc_name
fi
# Run the Python script with the arguments using pm2
echo "Running $script with the following arguments with pm2:"
echo "${args[@]}"
pm2 start "$script" --name $proc_name --interpreter python3 -- "${args[@]}"
echo ""
else
echo ""
fi
# Wait for a while before the next check
sleep 5
done