I'm looking to automate some tasks on my Ubuntu system using Bash or Python. I want a script that does the following:
1. Gets my internal IP address and prints it out.
2. Creates a cron job that checks every 10 seconds if I'm connected to the internet. If I am, it should append my public IP address to a file named SSH_IP.txt.
3. It should also check if the SSH service is running – if it's not, the script should start it and log the status to the same file.
4. The script needs to identify my nVidia GPU model, check if the latest driver is installed, and log the current driver version along with an 'Update' note if necessary.
5. I want it to wait for 2 minutes, but I'm confused about how to do that since the cron job runs every 10 seconds, and I might end up with overlapping processes.
6. Finally, if the SSH_IP.txt file exceeds 100KB, it should zip the file and copy it to a specified remote IP address.
Any insights on how to solve this? Thanks!
3 Answers
If this is for a class assignment, it might help to reformat your question for clarity. Start with a clear outline of each step required and what tools you can use. It's not uncommon for homework questions to be poorly phrased, so breaking it down can help you understand what's being asked better! Also, the option to use either Bash or Python for the script adds some flexibility. Good luck!
This script seems pretty straightforward! It's not necessarily tricky; you can find solutions online for each of these elements individually. Remember, the sleep command just pauses the script but doesn’t stop it from running. You can still have the cron job operating in the background while waiting periodically.
Regarding the cron job conflict you mentioned: cron jobs can only run every minute at best. If you want something to execute every 10 seconds, you might need to write it as a loop within your script and manage the timing yourself. Just keep in mind that a 2-minute wait might cause overlapping if cron is still trying to run the job every 10 seconds. You could use a 'flock' command to handle concurrency instead.
Using 'flock' is a solid approach! It can help you avoid those overlaps. Your idea of running a loop for a minute and then sleeping is definitely on the right track. Just remember to control how many instances of your script are running simultaneously!