userdata.sh 1.2 KB

123456789101112131415161718192021222324252627
  1. #!/bin/bash
  2. # AWS EC2 instance startup script https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
  3. # This script will run only once on first instance start (for a re-start script see mime.sh)
  4. # /home/ubuntu (ubuntu) or /home/ec2-user (amazon-linux) is working dir
  5. # Use >300 GB SSD
  6. cd home/ubuntu
  7. if [ ! -d yolov5 ]; then
  8. echo "Running first-time script." # install dependencies, download COCO, pull Docker
  9. git clone https://github.com/ultralytics/yolov5 -b master && sudo chmod -R 777 yolov5
  10. cd yolov5
  11. bash data/scripts/get_coco.sh && echo "COCO done." &
  12. sudo docker pull ultralytics/yolov5:latest && echo "Docker done." &
  13. python -m pip install --upgrade pip && pip install -r requirements.txt && python detect.py && echo "Requirements done." &
  14. wait && echo "All tasks done." # finish background tasks
  15. else
  16. echo "Running re-start script." # resume interrupted runs
  17. i=0
  18. list=$(sudo docker ps -qa) # container list i.e. $'one\ntwo\nthree\nfour'
  19. while IFS= read -r id; do
  20. ((i++))
  21. echo "restarting container $i: $id"
  22. sudo docker start $id
  23. # sudo docker exec -it $id python train.py --resume # single-GPU
  24. sudo docker exec -d $id python utils/aws/resume.py # multi-scenario
  25. done <<<"$list"
  26. fi