Run script to setup environment in VSCode after connecting to container or before compling/debugging - c++

I am working on a docker containerized c++ project that has defined shell scripts to setup the environment (using "source") for compiling and debugging. They have a lot of environment variables and could change at anytime so it would be hard to move them all into the launch.json file (and tedious to keep up with) so I need to call them before compiling or debugging.
The scripts only need to run once so if there was a way to run them after connection to the container that would be the best solution, however I cannot find anything like that.
I have tried to use the "preLaunchTask" in the launcher to run a task before debugging but it seems that the task's shell is different from the debug shell.
Is there anyway to handle this?
For the moment I am using a task to generate a .env file
printenv > ${workspaceFolder}/.preenv && . ${workspaceFolder}/setupEnv &&
printenv > ${workspaceFolder}/.postenv && grep -vFf
${workspaceFolder}/.preenv ${workspaceFolder}/.postenv >
${workspaceFolder}/.env

I have VSCode mount a directory as the container's homedir, and then put a .bash_profile file containing the necessary setups (or other suitable shell startup file) in that directory.
My /path/to/.devcontainer/devcontainer.json includes:
"mounts": [
"source=${localWorkspaceFolder}/.devcontainer/home,target=/home/username,type=bind,consistency=delegated"
],
"remoteUser": "username",
"userEnvProbe": "loginShell", // Ensures .bash_profile fires
Then my /path/to/.devcontainer/home/.bash_profile contains the necessary invocations to set my environment.

Related

autostart webserver and programm

I'm working on a Yocto based system. My problem is that I can't start my programm written in C++ and the webserver (node.js) at the same time right after the boot of my device.
I already tried this in /etc/init.d:
#! /bin/bash
/home/ProjectFolder/myProject
cd /home/myapp && DEBUG=myapp:* npm start
exit 0
I changed the rights after creating the script by
chmod +x ./startProg.sh
After that I linked it by
update-rc.d startProg.sh defaults
After reboot the system only starts the C++-programm. I tried some other possibilities like seperating the two comands in different shell-scripts, but that didn't work out any better.
Is there any option I missed or did I make any mistake trying to put those two processes into the autostart?
This of course isn't a C++ or Node.js question. A shell script is a list of commands that are executed in order, unless specified otherwise. So your shell script runs your two programs in the order specified, first myProject and when that's done npm will be started.
This is the same as what would happen from the prompt and the solution is the same: /home/ProjectFolder/myProject &

Cannot require luacom library in lua script

I am new to Lua programming language. I installed Lua for Windows v5.1.5-52. I want to use luacom library to run shell script. Here is my code,
local luacom = require('luacom');
local shell = luacom.CreateObject("WScript.Shell")
shell:Run ('echo 123', 0)
which throws the following error:
lua: COM exception:(.\src\library\tLuaCOM.cpp,398):The system cannot
find the file specified.
I looked for tLuaCOM.cpp file, but could not find it, not even folder src. Though I found luacom.dll in clibs folder.
Is there any workaround with this problem?
tLuaCOM.cpp is a luacom source file, so it's probably not on your PC, except you've build it yourself.
The error comes from one of the calls - either CreateObject() or Run.
The Run Method (Windows Script Host) helps says that it starts processes:
The Run method starts a program running in a new Windows process.
but echo is a shell command, not an executable, so you have to start an instance of the Windows command interpreter and pass your command like:
shell:Run('cmd /c "echo 123"')

my system V init script don't return

This is script content, located in /etc/init.d/myserviced:
#!/lib/init/init-d-script
DAEMON="/usr/local/bin/myprogram.py"
NAME="myserviced"
DESC="The description of my service"
When I start the service (either by calling it directly or by calling sudo service myserviced start), I can see program myprogram.py run, but it did not return to command prompt.
I guess there must be something that I misunderstood, so what is it?
The system is Debian, running on a Raspberry Pi.
After more works, I finally solved this issue. There are 2 major reasons:
init-d-script actually calls start-stop-daemon, who don't work well with scripts specified via --exec option. When killing scripts, you should only specify --name option. However, as init-d-script always fill --exec option, it cannot be used with script daemons. I have to write the sysv script by myself.
start-stop-daemon won't magically daemonize the thing you provide. So the executable provided to start-stop-daemon should be daemonized itself, but not a regular program.

How do I make a number of looping scripts execute at startup?

I have a few Python scripts, all of them involving while True: and a wait timer so they run at varying intervals. They do things like monitor a serial port and look for new versions of my code on a remote server. I haven't used cron because some require offsets (e.g run at ten seconds past the minute) and I wanted to keep things very simple.
Using rc.local, I run hook.py on startup. What can I put in hook.py to run a.py, b.py and c.py simultaneously and continuously? I tried subprocess (with shell = True) but I'm not sure the next line / next subprocess command will execute until the first one finishes - which will never happen. Plus it has some weird behaviour I'm struggling to debug (I can rw files using their absolute paths if I run the script directly; when subprocess runs them, it can't find the files).
Any suggestions? Just want something simple that can simultaneously execute several new python scripts. Platform is a Raspberry Pi.
Alternatively: if there's code I can put in rc.local that will spawn a new python process for all .py files in a specified directory, that would work too.
This sounds like it would be better suited for spawning via cron instead of an infinite while loops.
But if you want to continue running them in rc.local just put the & at the end of your command:
/usr/bin/python /home/you/command.py &
This runs the command in the background.
If you want to run all Python files in a given directory I would write a bash script like:
for file in /home/you/*.py
do
if [ "$?" == "0" ]
then
/usr/bin/python "$file" &
fi
done
We will need more information about your path issues to tell you more.

How to run a linux script before launching gdb debugging in Eclipse

How do I give commands to run before starting gdb debugging in Eclipse ?
Actually I want to execute few scripts that set environment variables (export vars) and execute a bunch of other programs before gdb process is launched from eclipse to debug my program.
I tried doing the following in debugger tab option:
<command> && <path-to-gdb-executable>
But I got the error that eclipse cannot execute gdb as given in above statement.
Please help - I actually want to execute a script called "before-launch-commands.sh" before debugging is started by gdb. I am trying to execute a cpp program under eclipse kepler.
Thanks.
The Eclipse Debug Configurations can already setup environment variables for you. I'm going to assume that that isn't sufficient, or you'd have already done it.
The first thing to do is create a new script, wrapped-gdb.sh:
#!/bin/sh
# Export any variables we need.
# Note that '.' (dot) is like an "include" statement.
. /path/to/before-launch-commands.sh
# Run GDB using the parameters passed in
exec /path/to/gdb "$#"
Next, set that script executable:
chmod +x /path/to/wrapped-gdb.sh
Finally, go to the Debugger tab in the debug configuration dialog, and in the box marked "GDB Debugger" enter /path/to/wrapped-gdb.sh.
When you launch your debug session it should now Do The Right Thing.