Stdout & stderr not redirecting on autostart - bash

I am using Raspbian (Debian with LXDE on a Raspberry Pi).
I have created the following two files. The first one is a .desktop file so as lxde can autostart my script, and the second one is the script in question.
The problem is that when I manually start the script it works perfect, creating the directories and redirecting the streams. However when I reboot the pi, and the script autostarts I get no output at all. The script is surelly working as my final app indeed starts. Only the streams are not there.
I have no idea for what to search for, or what causes this...
.desktop
[Desktop Entry]
Type=Application
Exec=system_start.sh
system_start.sh
#!/bin/bash
cd ~/application.linux64/
mkdir system_log
DIR=system_log/$(date +%Y%m%d)
mkdir $DIR/
./start.sh 1> $DIR/$(date +%T)operation_log.txt 2> $DIR/$(date +%T)errors_log.txt

I had this same problem with Linux Mint. A working command with redirect to a file did not work when started at boot using autostart .desktop file.
Enclosing the command in bash -c " " helped:
bash -c "/home/huehuehue/myguiapp >> /home/huehuehue/myguiapp.log 2>&1"

You should probably use the whole path instead of a relative path to make your script work in any circumstances and avoid ~:
#!/bin/bash
DIR=/home/username/application.linux64/
mkdir $DIR/system_log
SUBDIR=system_log/$(date +%Y%m%d)
mkdir $SUBDIR
./start.sh 1> $SUBDIR/$(date +%T)operation_log.txt 2> $SUBDIR/$(date +%T)errors_log.txt

Related

Problems in running a script.sh in Ubuntu app Windows10

I'm trying to learn how to code. I have to say that I'm using the Ubuntu app on the Windows system, so I don't know if my problems are related to this system.
I established these variables in the terminal
FOLDER="/mnt/c/Users/franc/Desktop/nuova"
species=mm10
fragmentsize=200
window=200
gap=200
output="/mnt/c/Users/franc/Desktop/nuova/sicer"
and then I wrote this loop
#!/bin/bash
for fq in $FOLDER/*.bam
do
bedtools bamtobed -i "$fq" > "${fq%.bam}.bed"
sicer -t ${fq%.bam}.bed \
-s $species \
-f $fragmentsize \
-w $window \
-g $gap \
-o $output
echo "DONE"
done
Basically I want the files in the FOLDER to be transformed in "${fq%.bam}.bed" and then I want to run sicer tool on these new files.
If I copy and paste these commands, on the terminal, everything goes fine but if I save the loop as script.sh and I try to run the script I obtain different errors.
Of course, I made the script executable with chmod +x and I also changed the syntax of the script using awk '{ sub("\r$", ""); print }' myscript.sh > myscript1.sh since I edited it in Windows(otherwise ubuntu fails to open it).
But when I launch the script containing the loop, it says that it is not able to open the files in the FOLDER (Failed to open BAM file /*.bam or BAD permission denied). I tried both to open it just giving the command ./myscript1.sh or also using sudo ./myscript1.sh.
What I'm missing? I have in some way link the variable I establish in the terminal to a new variable in the script saved?
thanks
Francesca
You need to export the variables so that they'll be inherited by the shell process running the script.
export FOLDER="/mnt/c/Users/franc/Desktop/nuova"
export species=mm10
export fragmentsize=200
export window=200
export gap=200
export output="/mnt/c/Users/franc/Desktop/nuova/sicer"

Exporting ROS Master URI from shell script

I am trying to export ROS_MASTER_URI from a shell script and then launch roscore. In my .sh file I have:
roxterm --tab -e $SHELL -c "cd $CATKIN_WS; $srcdevel; export ROS_MASTER_URI='http://locahost:1234'; roscore -p 1234"
When I do this, however, I get the following error in the roscore tab:
WARNING: ROS_MASTER_URI [http://locahost:1234] host is not set to this machine.
When I echo the ROS_MASTER_URI in this tab, it says that it is localhost:1234, which is correct. When I manually execute these commands, it works correctly and roscore launches without any issues. I am not sure why it does not work when launched from a bash file.
It was just a typo- missed the l in localhost. All working now.

Xubuntu, make a launcher for a .sh

I use Xubuntu with gnome-terminal.
I need to launch a sh script (restart.sh) on the pc startup.
The .sh file is in this directory: "/home/stark/Desktop/Tracking/Release/5600/"
I created a launcher like this:
[Desktop Entry]
Name=My GUI App
Exec=gnome-terminal -x sh -c "/home/stark/Desktop/Tracking/Release/5600/restart.sh"
Icon=/path/to/you/icon.svg
Terminal=true
Type=Application
StartupNotify=true
Name[en_US]=Intel5600
Comment=
Path=
What's wrong? Thank you!
EDIT:
This is the bash file:
#!/bin/bash
gnome-terminal -x sh -c 'python3 main00.py'
If I start it from its folder it works (if in a terminal I type: ./restart.sh), but from the launcher it doesn't work.
I tried to set terminal=false, but nothing changes.
add an #reboot cron task:
Running crontab -e will allow you to edit your cron.
Adding a line like this to it:
#reboot /path/to/script
will execute that script once your computer boots up.
vi /etc/rc.local
with content like the following:
# This script is executed at the end of each multiuser runlevel
/path/to/my/script.sh || exit 1 # Added by me
exit 0

How to cd, then append to a file from inside a shell script?

Here is a portion of my shell script:
...
docker exec -it graphite bash
cd /opt/graphite/conf
echo >> storage-schemas.conf
echo "[atlas]" >> storage-schemas.conf
echo "pattern = test.atlas" >> storage-schemas.conf
echo "retentions = 1s:15d,10s:45d" >> storage-schemas.conf
...
I want to bash into a running docker container (called graphite),
then cd into /opt/graphite/conf
then append a few lines of text to a file called storage-schemas.conf.
But when I run the above shell script, it creates a new file storage-schemas.conf on my desktop (which is my working directory) and appends to that!
I know I'm not using cd command correctly as this post here explains. But I really need my shell script to not have any dependency, as the end goal is to provide a one-click solution across many teams. Is there a way?
So it turns out that the work-around for this problem was fairly simple. Instead of entering inside the docker container and then trying to append a file, I'm now copying the file from host and pasting(overwriting) it inside the docker container. I can achieve this easily using the docker cp command and I don't have to leave my main bash script (Thanks again Marc for pointing this out).

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"

Resources