I would like to execute a backup script with rsync everytime a specific hard drive is plugged to my computer on ubuntu 16.04.
However I would like the user to be prompted if the backup should run or not and then he should be able to see the rsync output live in a terminal.
What I did so far:
I created a rule at /etc/udev/rules.d/backup_on_mount.rules
ACTION=="add", ATTRS{idVendor}=="1058", ATTRS{idProduct}=="0820", RUN+="/path/to/my/script/backup_on_mount.sh"
I created the backup_on_mount.sh script and made it executable
However I did not succeed in popping a new terminal window with my script running in it when I plug my hard drive.
Does anybody have an idea on how to begin ?
Thank you
You are not supposed to run anything that long from udev rules directly. One way of dissociating your long running script from the udev rule is by scheduling it (with at -M now command for example) instead of running it directly. The proper way of finding the right DISPLAY to open the new terminal depends on your desktop environment
Related
I am new to linux and I recently started to use arch linux (with KDE).
I am trying to set up a simple script that, at the startup, should en thunderbird and put it in the tray.
To this end, I added in the folder
/home/dario/.config/autostart-scripts
a file named
thunderbird-script
containing the following commands
sleep 10 kdocker thunderbird &
(of course, I have kdocker installed)
However, the script does not do the job.
Somebody can explain me why it does not work and how I should proceed?
I am looking for a strategy suggestion.
I am very new to Linux shell scripting. Just learning tcsh not more than a month.
I need a script to automatically detects when is the result files are done copied back from a remote server to a folder in a remote machine, then start scp the files back to my workstation.
I do not know in advance when the job will finish run, so the folder could have no result files for a long while. I also do not know when will the last result file done copied back from remote server to the folder (and thus can start the scp).
I had tried crontab. Work fine when I guess correctly, most of the time just disappointing.
So I tried to write a script myself and I have it now. I intend to produce a script that serves me and my colleagues too.
To use the script, the user first need to login to the remote machine manually. Then only execute the script at remote machine. The script first asks for user to input their local machine name and directory where they wish to save the result files.
Then the script will just looping to test when is the total number of files change. When it detected that, which means the first result file is starting to be copied back from the remote server, then it loops again to detect when is the total files size in the folder stop changing, which means last result file is finished copied to the folder. After that it executes scp to send all the result files to the user workstation, at the initially specified directory.
Script works fine but I wish to make the script able to run in background and still running by itself even if the user logout from the remote machine and close the terminal. And also I wish to let the user just type in a simple command in terminal to start the script, something like a simple
./script.tcsh
I tried to run the script by command
./script.tcsh &
but fails, because background process unable to accept user input.
Google and found something called disown, but the command is not found. Apparently the remote machine and my machine does not support this command.
Tried to modify the script to first accept the user input, then attempt to use
cat > temp_script.tcsh << EOF
{rest of my script}
EOF
and then a line of
./temp_script.tcsh &
to try to create another script file and use the first script to initiate the second script in background. Also fail, because cat does not treat $variable as a literal text, it replaces it with values. I have a foreach i(1 2) loop, and the cat command just keep reporting error (missing value of variable i, which is just a counter in foreach loop syntax).
I am out of idea at the moment.
Can anyone enlighten me with some strategy that I can try myself?
The goal is to use only 1 script file, and prompt user for 2 inputs (machine name and directory to save), then no more interaction with user or waiting, and able to run even closing the terminal.
Note: I do not need password to login to remote machine and back.
First of all, I am not an expert programmer, and I don't know much of the programmer's lingo. So please bear with me.
I am using Cygwin on windows, to copy a file from home directory to a remote server (which uses Linux) using SCP. I need to do this every day and so I want to automate it. I know how to schedule tasks in task scheduler, but I don't know what kind of file to save an scp command as. Please help? Oh and I don't have admin access, so I cannot install or use third party applications
First add Cygwin to your Windows Environment Variables. You can find directions on how to do that here (the directions you need are almost all the way at the bottom of the page). Now you should be able to run Linux commands from the command prompt. Simply make a .bat file in notepad with the commands you need to run. It should look something like this:
scp /cygdrive/d/test.txt <linux ip>:/etc/var/test/test.txt
Then use task scheduler to run the .bat file.
I need to create an apple script to perform DD Commands in sequence, to replicate the same .img file to multiple USB flash drives
The context: the computer is connected to a powered USB hub with 7 usb flash drives connected and unmounted by diskutil. No other USB devices or disk are connected to the computer (so the USB Sticks will be assign a disk1 - disk7 resource mapping)
The steps
a file named "source.img" will be prepared and put on the desktop (say an .img of a bootable OS)
terminal must be asked to change directory to the desktop
with administrative privileges (the password can be inserted into the script, or asked once to the user) it must be asked to perform:
"sudo dd if=source.img of=/dev/rdisk1 bs=1m"
the script must wait until the operation is concluded, then ask the terminal to perform another DD, to disk2 this time:
"sudo dd if=source.img of=/dev/rdisk2 bs=1m"
and again, wait until the operation is concluded, and then ask:
"sudo dd if=source.img of=/dev/rdisk3 bs=1m"
and so on with rdisk4, rdisk5, rdisk6, rdisk7.
at the end of the disk7 operation , the script can shut down the terminal and send a finder message to the user (or an audio notification) that the USB Duplication process to the 7 flash drives is concluded.
It's a way to create a "USB duplicator on the cheap" for bootable images to be put on multiple sticks, I need it for a school project to my students.
Anybody can help ? I am a zero with Applescript. And this thing will be useful for many.
Thank you !
Alberto
Why do you need to do it using Applescript? Why not a simple shell script like this?
#!/bin/bash
cd ~/Desktop
for i in {1..7}; do
echo "Duplicate to /dev/rdisk${i} (y/n) ?"
read r
[[ "$r" == "y" ]] && echo sudo dd if=source.img of=/dev/rdisk${i} bs=1m
done
say "Duplication complete"
Save it on your Desktop as "Duplicator" and then start a Terminal and in the new Terminal window type:
chmod +x Desktop/Duplicator
to make it executable. Then you can run it by double-clicking the "Duplicator" icon on your Desktop.
Note:
At the moment, it just echoes the "dd" command, run it and see if you like it then remove the "echo" if all looks correct.
Note:
This works on Mavericks and Mountain Lion at least, on some older versions of OSX you may need to name the script "Duplicator.command" and/or associate the script with the Terminal app. See here.
Note:
You might try adding a "&" after the "bs=1m" so that the 7 "dd" commands run in parallel, and then adding a new line that says "wait" between the lines "done" and "say" to wait for all "dd" commands to complete. This will mean that you can answer the 7 questions immediately one after the other instead of having to sit there and watch and do them one at a time. It may also make the "dd" commands run faster or slower - depending on the relative performance of your memory sticks and USB bus. It will also avoid problems with "sudo" timing out - though these could be solved by changing the timeout in the file /etc/sudoers.
I want to execute automator script when user plug in a pendrive with specific name.
How can I do it?
Applescript and automator cannot do this. However launchd can. Launchd can watch folders, and if something changes in the folder then launched will run your command. In your case you would want to watch the /Volumes folder. When you mount the USB drive it's mount point will be that folder. Therefore you need to write yourself a launchd plist file, and your command could be to run your automator script. Just google for more info on launchd. I did a quick search and this link gives most of the information you need.
Unless I'm missing something, I believe that Applescript can in fact do what you want. A Folder Action listening to the Volumes directory (as per Auto-Detect Mounted Volumes) will run any time a volume is mounted. You can then run a shell script (or some other method) that detects if the mounted drive is your drive, and run your workflow if so (or perhaps more easily, exit if not).