ADB Commands automation - shell

I am trying to run the following commands in Cygwin:
For Logcat dump:
adb devices
adb logcat –b all –c
adb logcat -b main -b events -b radio -b system -v threadtime > logcat.txt
For tcp dump:
adb root & adb wait-for-device & adb shell chmod 755 /data/
adb shell ifconfig && adb shell tcpdump -i any -w /data/youtube_server.pcap
adb pull /data/youtube_server.pcap C:\Temp\youtube_server.pcap
One thing I did is opening two terminals and run both in parallel. This works perfectly fine and I get the data both for TCP dump and logcat from android. However, I would like to know that it is possible to run the whole commands in one shell script. I tried writing them in shell script but once I wrote the logcat on the top and tcp down, it runs till logcat only. When I stop it, then it starts to collect the data. Please help me

Related

adb shell command not working inside a bash script

I am trying to write a bash script which when run can find the ip address of the device connected through usb. The bash script which is doing this:
#!/usr/bin/bash
ip=$(adb shell 'sudo ip -f inet addr show')
Then I am going to use the ip later in the script. But the shell command is giving me an error:
error: closed
To call the script I just navigated to the directory with terminal where its situated and entered
./name_of_script
Every shell command is giving me this error. I even tried removing the single quotes. When I tried running the same command outside the script inside a terminal, it worked flawlessly. How to run shell commands in a bash script?
What's wrong?
On Android there is no sudo, only on rooted devices the su binary is present. Therefore you have to change the command to:
adb shell su -c 'ip -f inet addr show'
But in my tests the used command ip -f inet addr show did not need root permissions, the output was the same.
So you can simplify the command to:
adb shell ip -f inet addr show

Cygwin run multiple commands at once

MATLAB runs on a host machine. By using the 'system' call and CYGWIN I have to run some applications on a remote system based on linux.
The problem is, after calling the SSH command the other commands are ignored
so
system('C:\cygwin64\bin\bash -l -c "ssh -t -t 10.0.0.127; cd /home/superuser/MAGIC_PATH")
does not work
So I tried to change the directory sequentially after the SSH-Connections, but now the MATLAB-script is blocked. And I have to type the command manually. Which is not the desired solution
In MATLAB:
cygwin_path='C:\cygwin64\bin\bash';
binary_path='/home/superuser/MAGIC_PATH';
SSH_string=sprintf('%s -l -c "ssh -t -t %s &"',cygwin_path,remote_IP)
ChangeDIR_string=sprintf('%s -l -c "cd /home/superuser/"',cygwin_path)
So how can I change my code respectively the system call, so that it automatically runs multiple commands and starts some applications (as background jobs)

Windows 7 Task Scheduler BASH Script Fails

In order to use rsync I created a BASH script. It runs fine from the Cygwin shell in WIN 7 but fails when run from the WIN 7 Task Scheduler. My Task Scheduler Script is a simple:
c:\cygwin\bin\bash.exe -l -c "~user/rsync_Windows_Backup 2>&1 >> ~user/Documents_cron.log"
The initial directory is set to C:\Cygwin\bin.
My BASH script is a typical rsync command with [options] SRC DEST and some related housekeeping.
The rsync command within the "rsync_Windows_Backup" BASH script is:
/bin/time -f "\nElapse (hh:mm:ss.ss) %E" \
rsync.exe -v -rltz --chmod=a=rw,Da+x -u "$SRC" "$DEST" >> "$LOG" \
2 >> "$LOG"
$ ./rsync_Windows_Backup - succeeds.
But the Task Scheduler Job fails carping that it cannot find the DEST Folder that the BASH script references. When I do a "cd DEST" from the BASH command line the Folder is avialable and can be written to.
I should add some more details that the sender is a WIN 7 desktop that is mapped to a Vista desktop receiver with a drive mapping J:. The BASH script does start but fails with:
rsync: writefd_unbuffered failed to write 4 bytes to socket [sender]: Broken pipe (32)
rsync: mkdir "/cygdrive/J/DocumentsBackup" failed: No such file or directory (2) rsync error: error in file IO (code 11)
I have tried several ideas to influence how WIN 7 handles mappings and permissions assuming this is the root of the problem. So far nothing seems to help.
Another characteristic is that the exact same BASH script and Task Scheduler Job does succeed it WIN Vista Business Edition. So I am assuming there is something in WIN 7 that I am missing.
I am stumped and could use some guidance.
Thanks.
I now have this working in Win 7 from the task scheduler as I need. Thank you to #netubsi and #firerat of LinuxQuestionsorg and #konsolebox for the suggestions that lead to a solution.
Here is what I did:
cmd /c net use T: '\\server\share' # Created a separate temporary share for Cygwin
DEST="/cygdrive/T/User/FolderBackup/" # Use temporary Share in Destination
rsync -avuz --copy-links "$SRC" "$DEST" # Do backup
cmd /c net use T: /delete # Remove temporary share
It appears that in WIN 7 the share created in Windows is NOT available to a Cygwin script, IF it is launced from the Win 7 task scheduler. It IS available if the script is launced from the Cygwin command line. It also appears that this is NOT an issue in Win Vista.
This seems odd to me. Perhaps there is another explanation that I am missing. However I am just relieved to have this working!!
You can also just use the network address directly in cygwin:
DEST="//server/share/User/FolderBackup"
Cygwin mounts local and mapped drives under /cygdrive. Using taskscheduler in win7 if you list the contents of /cygdrive, all you will see are local drives???
First option is to run your script as
c:\cygwin\bin\bash.exe -l -c "~/rsync_Windows_Backup >> ~/Documents_cron.log 2>&1"
If you want to capture the stderr output as well, you have to place it in front to copy the fd of the file, and not of stdout.
Make sure that rsync_Windows_Backup has executable permissions. Running ls -l ~/rsync_Windows_Backup should show it.
If it doesn't work, try to use absolute paths. On your Cygwin screen where the current direcory shows ~ in the prompt type pwd which would show something like
User#System ~
$ pwd
/home/User
Basing from that as an example your command should now be like:
c:\cygwin\bin\bash.exe -l -c "/home/User/rsync_Windows_Backup >> /home/User/Documents_cron.log 2>&1"

How to strace adb shell am start myandroidapp

I need to lean on you for some help on stracing android apps in the sdk emulator
here is my setup
android sdk emulator running android api 4.03
adb shell connected to emulator.
I am able to install an apk usng adb install filename.apk
I am able to run the app using
adb shell
am start -a android.intent.action.Main -n com.akproduction.notepad/com.akproduction.notepad.NoteList
I try to strace using (adb shell)
strace am start -a android.intent.action.Main -n com.akproduction.notepad/com.akproduction.notepad.NoteList
but I get nothing!
how do you trace the runtime behavior of android apps and their installation ?
thanks,
Jose.
p.s. the test app is located here: http://www.appbrain.com/app/ak-notepad/com.akproduction.notepad
am is a batch file and cannot be used in strace.
You need to run it like this:
strace -v -fF -tt -s 65535 -o /data/local/tmp/opengl.strace /system/bin/app_process /system/bin com.android.commands.am.Am start -a android.intent.action.MAIN -n demo.opengl.android/.OpenGLDemo
BTW don't use the strace statically compiled binary available for download from:
http://benno.id.au/blog/2007/11/18/android-runtime-strace
It will output nothing.
Instead use the one which comes with the ROM.

How to run a command in background using ssh and detach the session

I'm currently trying to ssh into a remote machine and run a script, then leave the node with the script running. Below is my script. However, when it runs, the script is successfully run on the machine but ssh session hangs. What's the problem?
ssh -x $username#$node 'rm -rf statuslist
mkdir statuslist
chmod u+x ~/monitor/concat.sh
chmod u+x ~/monitor/script.sh
nohup ./monitor/concat.sh &
exit;'
There are some situations when you want to execute/start some scripts on a remote machine/server (which will terminate automatically) and disconnect from the server.
eg: A script running on a box which when executed
takes a model and copies it to a remote server
creates a script for running a simulation with the model and push it to server
starts the script on the server and disconnect
The duty of the script thus started is to run the simulation in the server and once completed (will take days to complete) copy the results back to client.
I would use the following command:
ssh remoteserver 'nohup /path/to/script `</dev/null` >nohup.out 2>&1 &'
#CKeven, you may put all those commands on one script, push it to the remote server and initiate it as follows:
echo '#!/bin/bash
rm -rf statuslist
mkdir statuslist
chmod u+x ~/monitor/concat.sh
chmod u+x ~/monitor/script.sh
nohup ./monitor/concat.sh &
' > script.sh
chmod u+x script.sh
rsync -azvp script.sh remotehost:/tmp
ssh remotehost '/tmp/script.sh `</dev/null` >nohup.out 2>&1 &'
Hope this works ;-)
Edit:
You can also use
ssh user#host 'screen -S SessionName -d -m "/path/to/executable"'
Which creates a detached screen session and runs target command within it
What do you think about using screen for this? You could run screen via ssh to start the command (concat.sh) and then you'd be able to return to the screen session if you wanted to monitor it (could be handy, depending on what concat does).
To be more specific, try this:
ssh -t $username#$node screen -dm -S testing ./monitor/concat.sh
You should find that the prompt returns immediately, and that concat.sh is running on the remote machine. I'll explain some of the options:
ssh -t makes a TTY. screen needs this.
screen -dm makes it start in "detached" mode. This is like "background" for your purposes.
-S testing gives your screen session a name. It is optional but recommended.
Now, once you've done this, you can go to the remote machine and run this:
screen -r testing
This will attach you to the screen session which contains your program. From there you can control it, kill it, see its output, and so on. Ctrl-A, then d will detach you from the screen session. screen -ls will list all running sessions.
It could be the standard input stream. Try ssh -n ... or ssh -f ....
For me, only this worked:
screen -dmS name sh my-script.sh
This, of course, depends on screen, and lets you attach later, if you ever want stdin or stdout. Screen will terminate itself when my-script.sh ends.
Below is a much more common decision that required some efforts to find, and it really works for me:
#!/usr/bin/bash
theScreenSessionName="test"
theTabNumber="1"
theStuff="date; hostname; cd /usr/local; pwd; /usr/local/bin/top"
echo "this is a test"
ssh -f user#server "/usr/local/bin/screen -x $theScreenSessionName -p $theTabNumber -X stuff \"
$theStuff
\""
It sends $theStuff list of commands to the tab No $theTabNumber of the screen session $theScreenSessionName preliminarily created at the 'server' on behalf of 'user'.
Please be aware of a trailing whitespace after
-X stuff \"
that is sent to overcome a 'stuff' option's glitch. The whitespace and $theStuff in the next line are appended by 'Enter' (^M) keystrokes. DON'T MISS 'EM!
The "this is a test" message is echoed in the initial terminal, and $theStuff commands are really executed inside the mentioned screen/tab.

Resources