1

Operating system: Xubuntu

I'm trying to create a bash script that takes a screenshot of my screen and uploads the resulting file to a server. I tried many ways and even asked ChatGPT but still can't find a working solution. Could you please point me to the right direction? Thanks

This is the code I am using

#!/bin/bash

# Set the FTP server hostname or IP address
FTP_SERVER=xxx

# Set the FTP username and password
FTP_USERNAME=xxx
FTP_PASSWORD=xxx


# Set the local and remote file paths
LOCAL_FILE=/home/nicola/screenshot.png
REMOTE_FILE=/public_html/screenshots/screenshot.png

# Connect to the FTP server and log in
ftp -inv $FTP_SERVER << EOF
user $FTP_USERNAME $FTP_PASSWORD

# Send the file
put $LOCAL_FILE $REMOTE_FILE

# Quit
bye
EOF

The file is correctly sent, now I need to repeat the operation once every second but the connection to the server should not be repeated I guess, so it should stay outside the loop.

The files will not be overwritten, I intend to use Date or Time and append it to the filename

Nicola
  • 123
  • 4
  • How long does it take to upload one file to the server? – roaima Dec 20 '22 at 23:45
  • @roaima that's a good question and I may want to figure out a solution to that in a later moment but now I even can't achieve what I need at first – Nicola Dec 20 '22 at 23:50
  • It seems to me you're asking two totally different questions. One is how to take screenshot, and the other one is how to send a file to ftp every second. Am I right? It's not entirely clear from your question. Maybe you should explain where exactly you're stuck. I'm also not sure what you mean by _"connection to the server should not be repeated"_ and _"should stay outside the loop"_ – aviro Dec 21 '22 at 00:16
  • @aviro thanks for your reply, I know how to take a screenshot, I'd just use scrot and to prevent file overwriting I'd just use a timestamp appended to the filename, the code I pasted is a simplified version but the part that I can not figure out how to do is a working loop that sends the file to the server once every second or even every 10 seconds, the interval is not important at this moment. "connection to the server should not be repeated" means that when I asked ChatGPT it gave me a working code but the ftp connection was inside the loop and this seemed sub-optimal to me. Sorry for my EN – Nicola Dec 21 '22 at 00:20
  • @Nicola you asked this same question earlier ...why are you asking again? – jsotola Dec 21 '22 at 01:03
  • @jsotola because the previous question was not clear enough and editing did not help – Nicola Dec 21 '22 at 01:05

1 Answers1

3
  • add time postfix is easy: REMOTE_FILE="/public_html/screenshots/screenshot-$(date +"%Y%m%d_%H%M%S.%N").png"
  • for continuous ftp connection you can try CurlFtpFS.

in general it is bad idea to use ftp. It is outdated and insecure. If you can ssh to remote server just use scp or sshfs/rclone.

It is usually a pain to maintain long-live connection. Even with sshfs or rclone you will end up frozen file system. Thus I really recommend you transfer screenshot in a pulsed way: collect and cache the images for a few minutes then start a transfer as background job, something like:

REMOTE_URL="<usr>@<server>:<path>/"
group_size=120
interval=1
# group_id can be the outer loop count
group_id=0
while true
do
  mkdir $group_id
  for _i in $(seq 1 $group_size)
  do
    filename="screenshot-$(date +"%Y%m%d_%H%M%S.%N").png"
    # whatever screenshot command you choose, here I use grim as example
    grim "$group_id/${filename}"
    sleep ${interval}
  done
  # run commands in background: copy all files in current subdir and remove the current dir after job is done.
  (scp "$group_id"/* "${REMOTE_URL}/${filename}" && rm -rf $group_id &)
  # the job is in background, so it won't block next screenshot.
  # you may see several copy jobs in parallel; if your network is slow.
  ((group_id++));
done
roaima
  • 107,089
  • 14
  • 139
  • 261
Wang
  • 1,212
  • 2
  • 15
  • 26
  • This is the best answer so far and I posted the same question even elsewhere. I just have to better study bash scripting but thanks for your suggestions – Nicola Dec 21 '22 at 01:04
  • I'd suggest using `date --utc` for the timestamp generation _unless_ you are in a timezone that does not jump forwards/backwards each year. – roaima Dec 21 '22 at 09:19