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

6
  • How long does it take to upload one file to the server? Dec 20, 2022 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, 2022 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, 2022 at 0: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, 2022 at 0:20
  • @Nicola you asked this same question earlier ...why are you asking again?
    – jsotola
    Dec 21, 2022 at 1:03

1 Answer 1

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
2
  • 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, 2022 at 1: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. Dec 21, 2022 at 9:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .