Linux programs usually have configuration files, if the configuration file uses a relative path, for example: "../src/config.xml"
then at the current Under the path, there is no problem with the execution;
If you switch to another path, there will be problems with the execution and the file cannot be found;
When writing the script, you must first cd to the executable file. Path, and then execute;
If you want to eliminate this effect, you can not write the dead path, then you need to transform the relative path into an absolute path;
Step:
1. First, get the path where the program is located;
2. Add the relative path, then get the absolute path;
Please note: not the current path, getcwd can get the current path; Not the absolute path of the program; I made this mistake at the time!
The following describes how to get the path where the program is located:
Method 1. If you don't care about possible security risks, you can use procfs, then readlink, to put the current process's pid corresponding to the directory below. The location pointed to by file is read out (note that you need to mount procfs first)
pit_t mypid = getpid();
sprintf(strsrc, "/proc/%d/file", mypid);
readlink(strsrc, strdest, LEN);//LEN is best your _POSIX_PATH_MAX
Method 2. Use the realpath function, then dirname; finally splicing out the absolute path of the configuration file; Br>
char path[PATH_MAX];
char *rpath = realpath(argv[0], path);
LOG_IT(LOG_MAIN, LOG_DEBUG, "argv[0]: % s, realpath %s", argv[0], rpath);
char* base = basename(path);
char* dir = dirname(path);
LOG_IT(LOG_MAIN, LOG_DEBUG, "base: %s, dir %s", base, dir);
char conf_file[2048];
int maxlen_conf = 2048;
snprintf(conf_file, maxlen_conf, "%s /%s", dir, "../src/config.xml");
LOG_IT(LOG_MAIN, LOG_DEBUG, "conf_file %s", conf_file);
For systems on the Internet, it is important to be clear about the situation: the network is not se
In order to ensure the normal operation of the Linux system and accurately solve various system prob
There is no doubt that Red Hat is indeed the choice of many companies considering Linux, but in rece
That day, my colleague showed me an operation step on my computer, using an env command, well, I adm
Set up a personal Bluetooth LAN under Linux
Rescue mode after a serious fault in the Linux operating system
Solve the dual system boot option does not show the Linux system problem
Linux vsftp configuration tutorial
Remote access method for Linux and Windows operating systems
Fedora 15 installed nvidia driver
Create a mysql database note with c language under linux
Block SSH password attempt tool software
Linux ends with a keyword process (batch kill process method)
WindowsXP shutdown failure full contact, Window XP operating system tutorial
Running VS2005 Virtual Machine
Sharing the undo method of the five useless functions in the xp system
Directly reading the audio and video files in the compressed package
Win7 64-bit pure version of the system shutdown prompts "To return to windows" how to solve
Windows 7 power plan, the choice of four major programs
Manually add Flash support for Win8 Metro Edition IE10 browser
Win2000 optimized full version
Win10 system image installation failed "Is there a problem when running this tool" What should I do?